您的位置:首页 > 其它

map容器的使用以及打印Unicode宽字符到文本

2016-06-08 17:27 411 查看
static map<vector<unsigned short>,int> mapwordsTwogram; //定义全局二元组或三元组统计字符频率变量mapwordsTwogram;


FILE* fp = fopen( "XXX.txt", "w" );

int CountWords3gram(vector<vector<unsigned short>> count3gram )
{
cout<<"开始统计三元组中的字符频率"<<endl;
unsigned int i=0;
for(i = 0; i < count3gram.size(); i++)
{
if(count3gram[i][0]>= 0x4e00 && count3gram[i][0] <= 0x9fbb||count3gram[i][1]>= 0x4e00 && count3gram[i][1] <= 0x9fbb||count3gram[i][2]>= 0x4e00 && count3gram[i][2] <= 0x9fbb)
mapwordsTwogram[count3gram[i]]++;
}
cout<<"map :"<<i<<" end!"<<endl;
return i;

}

打印vector到文本中去,注意宽字符的处理方式

for(int i=0; i<vec2.size();i++)
{
//fprintf(fpstatic, "%c, %d\n", vec[i].first,vec[i].second );
printf("%d %d, %d\n", vec2[i].first[0],vec2[i].first[1],vec2[i].second );
_wsetlocale(0, L"chs");
//fwprintf(fp, L"%c ", (wchar_t)vec[i].first);
//fprintf(fp, "%d\n", vec[i].second);
//fwprintf(fp, L"%c%c ", (wchar_t)vec2[i].first[0],(wchar_t)vec2[i].first[1]); //二元组打印
fwprintf(fp, L"%c%c%c ", (wchar_t)vec2[i].first[0],(wchar_t)vec2[i].first[1],(wchar_t)vec2[i].first[2]); //三元组打印
fprintf(fp, "%d\n", vec2[i].second);

}

以及非常重要的如何按值对map进行排序

//sort函数比较方式
struct CmpByValue
{
bool operator()(const pair<vector<unsigned short>,int> & lhs, const pair<vector<unsigned short>,int> & rhs)
{return lhs.second > rhs.second;}
};

//转存map到vector
vector<pair<int,int> > vec(mapwords.begin() , mapwords.end());
//二元组map到vector
vector<pair<vector<unsigned short>,int> > vec2(mapwordsTwogram.begin() , mapwordsTwogram.end());

//开始排序
cout<<"sort vector2 start!"<<endl;
//sort(vec.begin(),vec.end(),CmpByValue());
sort(vec2.begin(),vec2.end(),CmpByValue());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: