string 转 wstring:
#include <iostream> #include <sstream> #include <locale> #include <string> #include <codecvt> int main() { std::string utf8_str = "你好,世界!"; std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; std::wstring wide_str = converter.from_bytes(utf8_str); std::wcout.imbue(std::locale("chs")); std::wcout << wide_str << std::endl; return 0; }
wstring 转 string:
#include <iostream> #include <sstream> #include <locale> #include <string> #include <codecvt> int main() { system("chcp 65001"); std::wstring wide_str = L"你好,世界!"; std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; std::string utf8_str = converter.to_bytes(wide_str); std::cout.imbue(std::locale("chs")); std::cout << utf8_str << std::endl; return 0; }
标签:std,string,c++,wstring,str,互转,include,converter From: https://www.cnblogs.com/nanfei/p/18392482