64,671
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <stdio.h>
#include <locale.h>
using namespace std;
string& ws2s(wstring &ws, string &str)
{
char *pch = (char *)malloc(ws.length() * 2 + 1);
const wchar_t *pwch = ws.c_str();
wcstombs(pch,pwch,ws.length() * 2);
str.clear();
str.append(pch);
return str;
}
int main()
{
setlocale(LC_ALL,"chs");
wstring ws = L"一二三";
ws.insert(ws.find(L'一') + 1,1,'a');
string s;
ws2s(ws,s);
cout<<s<<endl;
getchar();
return 0;
}
std::wcout.imbue(std::locale("CHS"));
wstring wStr = L"a一二三四五六七bc";
wstring::iterator iter = wStr.begin() + 3;
wStr.insert(iter, 'a');
for (int i = 0;i<wStr.size();++i)
{
int x = (int)&wStr[i];
cout<<x;
wcout<<wStr[i]<<' ';
cout<<endl;
}
std::wcout.imbue(std::locale("CHS"));
wstring wStr = L"一二三四五六七";
wstring::iterator iter = wStr.begin() + 3;
wStr.insert(iter, L'a');
for (int i = 0;i<wStr.size();++i)
{
wcout<<wStr[i]<<' ';
}
wstring wStr = L"一二三四五六七";
wstring::iterator iter = wStr.begin() + 3;
wStr.insert(iter, 'a');
string str = ws2s(wStr); // wstring转string
cout << str << endl;