关于几个统计用的C++方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linfengmove/article/details/73650482

最近要用到关键词的统计。在网上找了一些方法,所以总结一下,供参考。

利用map的特性进行同关键词的数量统计,模糊匹配的话可以把信息抽象成一个类,然后重载 < 操作符进行类型信息的区分.


	//统计相同组的同学数量
	struct Student
	{
		int id;
		std::string name;
		int group; //组
		std::string groupName; //组名
	};
	struct CompareVV_t
	{
		bool operator()(Student l, Student r)
		{
			return l.group < r.group;
		}
	};

	std::map<Student, int, CompareVV_t> statisticsVV;
	statisticsVV[Student]++;

	//统计组容纳人数最多的三个组
	typedef std::pair<Student, int> PAIR;
	std::vector<PAIR> vec(statisticsVV.begin(), statisticsVV.end());

	struct CompareNum
	{
		bool operator()(PAIR l, PAIR r)
		{
			return l.second > r.second;
		}
	};
	std::partial_sort(vec.begin(),vec.begin()+2, vec.end());

	//查找人数第三的组  vec.begin()+2
	std::nth_element(vec.begin(),vec.begin()+2, vec.end());

猜你喜欢

转载自blog.csdn.net/linfengmove/article/details/73650482