从一个std ::字符串转换为bool [英] Converting from a std::string to bool

查看:141
本文介绍了从一个std ::字符串转换为bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是转换为std :: string到bool的最佳方式?我打电话返回0或1的功能,我需要把这个变成一个布尔值,一个干净的解决方案。

What is the best way to convert a std::string to bool? I am calling a function that returns either "0" or "1", and I need a clean solution for turning this into a boolean value.

推荐答案

这很可能会矫枉过正你,但我会使用<一个href=\"http://www.boost.org/doc/libs/1_40_0/libs/conversion/lexical_cast.htm\">boost::lexical_cast

It'll probably be overkill for you, but I'd use boost::lexical_cast

boost::lexical_cast<bool>("1") // returns true
boost::lexical_cast<bool>("0") // returns false

这篇关于从一个std ::字符串转换为bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆