c++从string类型转换为bool类型

利用输入字符串流istringstream

bool b;
string s="true";
istringstream(s)>>boolalpha>>b;//boolalpha>>必须要加 
cout<<boolalpha<<b<<endl;

但当字符串s为“1”时,上面的代码无法正确转换,此时应该用:

string s="1";
istringstream(s)>>b;
cout<<boolalpha<<b<<endl;
原文地址:https://www.cnblogs.com/tianzeng/p/9038769.html