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;

 

posted on 2018-05-14 23:39  tianzeng  阅读(7088)  评论(0编辑  收藏  举报

导航