如何将wstring转换为字符串?
#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
相关分类