#include<iostream>
#include<locale>
#include<vector>
#include<string>
using namespace std;
wstring to_wstring(const string& str, const locale& loc=locale())
{
vector<wchar_t>buf(str.size());
use_facet<ctype<wchar_t>>(loc).widen(str.data(), str.data() + str.size(), buf.data());
return wstring(buf.data(), buf.size());
}
string to_string(const wstring& str, const locale& loc = locale())
{
vector<char>buf(str.size());
use_facet<ctype<wchar_t>>(loc).narrow(str.data(), str.data() + str.size(),'*', buf.data());
return string(buf.data(), buf.size());
}
int main()
{
string s("hello world!\n");
wstring wstr = to_wstring(s);
wcout << wstr << endl;
cout << to_string(wstr) << endl;
return 0;
}
C++实现string与wstring的互相转换
最新推荐文章于 2024-07-23 10:31:41 发布