C++本地化小结

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;
}


转载于:https://my.oschina.net/u/196018/blog/383790

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值