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

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());


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值