码迷,mamicode.com
首页 > 编程语言 > 详细

C++ bool和string转换

时间:2014-11-17 21:18:56      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:string   c++   类型转换   

直接贴代码吧,用g++可以编译,测试ok


#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char **argv)
{
    bool a = true;

    ostringstream os1;
    os1 << a;
    cout << string(os1.str()) << endl;

    ostringstream os2;
    a = false;
    os2 << a;
    cout << string(os2.str()) << endl;

    stringstream ss1;
    ss1 << true;
    cout << ss1.str() << endl;

    stringstream ss2;
    ss2 << false;
    cout << ss2.str() << endl;

    bool b;  
    string s = "true";  
    istringstream(s) >> boolalpha >> b;
    cout << "b = " << b << endl;

    s = "false";
    istringstream(s) >> boolalpha >> b;
    cout << "b = " << b << endl;
    
    return 0;
}
编译运行如下:

bubuko.com,布布扣

C++ bool和string转换

标签:string   c++   类型转换   

原文地址:http://blog.csdn.net/wzzfeitian/article/details/41217111

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!