如何将wstring转换为字符串?问题是如何将wstring转换为String?我有下一个例子:#include <string>#include <iostream>int main(){
std::wstring ws = L"Hello";
std::string s( ws.begin(), ws.end() );
//std::cout <<"std::string = "<<s<<std::endl;
std::wcout<<"std::wstring = "<<ws<<std::endl;
std::cout <<"std::string = "<<s<<std::endl;}带有注释行的输出是:std::string = Hellostd::wstring = Hellostd::string = Hello但没有只有:std::wstring = Hello这个例子有什么问题吗?我能像上面那样进行转换吗?编辑新的例子(考虑到一些答案)是#include <string>#include <iostream>#include <sstream>#include <locale>int main(){
setlocale(LC_CTYPE, "");
const std::wstring ws = L"Hello";
const std::string s( ws.begin(), ws.end() );
std::cout<<"std::string = "<<s<<std::endl;
std::wcout<<"std::wstring = "<<ws<<std::endl;
std::stringstream ss;
ss << ws.c_str();
std::cout<<"std::stringstream = "<<ss.str()<<std::endl;}产出如下:std::string = Hellostd::wstring = Hellostd::stringstream = 0x860283c因此,不能使用字符串流将wstring转换为字符串。
3 回答
- 3 回答
- 0 关注
- 390 浏览
添加回答
举报
0/150
提交
取消