从 std::string 转换为 bool [英] Converting from a std::string to bool

查看:37
本文介绍了从 std::string 转换为 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.

推荐答案

这对你来说可能有点矫枉过正,但我​​会使用 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::string 转换为 bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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