现在的位置: 首页 > 综合 > 正文

c++ string和wstring互相转化

2013年04月25日 ⁄ 综合 ⁄ 共 337字 ⁄ 字号 评论关闭

先立个帖, 慢慢记录.


wstring --> string:

wstring ws = L"hello";

string s(ws.begin(), ws.end());

//上面的构造函数等价于assign函数, 其实就是string s; s.assign(ws.begin(), ws.end());

char c[] = "hello";

cout << strcmp(s.c_str(), c) << endl; //输出0


string --> wstring:

string s = "hello world!";

wstring ws;

ws.assign(s.begin(), s.end());
//同理, 可以直接用构造函数

wstring ws2 = L"hello world!";

cout << (ws == ws2) << endl;  //输出1

抱歉!评论已关闭.