MinGW对c++的std::locale支持不佳,在MinGW程序下使用本地化功能要直接用c版本,setlocale(LC_ALL, "");
这样wchar_t就可以一次性读2个字节的数据了,否则wchar_t只可以一次性读一个字节。但是输出的时候,不能用wcout,只能用wprintf输出。
例如wprintf(L"%lc ",temp);
写一个小程序,统计文本中所有汉字出现的次数。
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
int main()
{
wifstream in("1.txt");
setlocale(LC_ALL, "");
wchar_ttemp;
map<wchar_t, int> cnt;
while(!in.eof())
{
in >> temp;
cnt[temp]++;
}
typedef map<wchar_t, int>::const_iterator map_iter;
for(map_iter it = cnt.begin(); it != cnt.end(); ++it)
{
wprintf(L"%lc ", it -> first);
cout << it -> second << endl;
}
return 0;
}