[C ++] - the number of fruit with a map appears statistics

The document questions before CVTE there have been such a problem, generally means that the statistics of the number of fruits appear, and then come before the k kinds of employees like to eat the fruit.
That problem here can be divided into two parts

- a first count is the number of times each fruit appears

There are three ways:

//第一种:
void test_map1()
{
	string strs[]={"苹果""香蕉""香蕉","苹果""香蕉""苹果""香蕉""香蕉""草莓"};
	map<string,int> countmap;
	for(auto str:strs)
	{
		auto ret=countmap.find(str);
		if(ret!=countmap.end())
		{
			ret->second++;
		}
		else
		{
			countmap.insert(pair<string,int>(str,1));
		}
	}
}
//第二种方法
void test_map2()
{
	string strs[] = { "苹果","香蕉","香蕉","苹果","香蕉","苹果","香蕉","香蕉","草莓" };
	map<string, int> countmap;
	for (auto str : strs)
	{
		//pair<map<string, int>::iterator, bool> ret = countmap.insert(pair<string, int>(str, 1));
		auto ret= countmap.insert(pair<string, int>(str, 1));  //无论是什么水果都进行插入
		if (ret.second == false)
		{
			ret.first->second++;
		}
	}
}
//第三种方法
void test_map3()
{
	string strs[] = { "苹果","香蕉","香蕉","苹果","香蕉","苹果","香蕉","香蕉","草莓" };
	map<string, int> countmap;
	for (auto str : strs)
	{
		countmap[str]++;
	}
}

The first two methods will be well understood, for the third method is mainly overloaded "[]", it would look a bit complicated, function prototype is as follows:

mapped_type& operator[] (const key_type&k)
{
	return (*((this->insert(make_pair(k, mapped_type()))).first)).second;
}

May be difficult to understand, then I drew a diagram, and finally returns iterator iterator, then dereference it returned iterator is an alias, and then take it the second it is easy to get value for value.
Here Insert Picture Description

- The second step is to issue a topk
number of occurrences of fruit are sorted in descending order, remove the first k elements

struct compare
{
	bool operater()(const pair<string, int>&l, const pair < string, int)&r)
	{
	return l.second > r.second;
	}
};
void test_map3()
{
	string strs[] = { "苹果","香蕉","香蕉","苹果","香蕉","苹果","香蕉","香蕉","草莓" };
	map<string, int> countmap;
	for (auto str : strs)
	{
		countmap[str]++;
	}
	vector<pair<string, int>> v;
	for (auto &e : countmap)
	{
		v.push_back(e);
	}
	sort(v.begin(), b.end(), compare());
	//将结果打印出来
	for (size_t i = 0; i < k; i++)
	{
		cout << v[i].first << ":" << v[i].second << endl;
	}
}
  • Here are a priority queue solution again
//利用优先级队列解决
void GetFavoriteFruit(const vector<string>&fruits, size_t k)
{
	//首先统计每种水果的次数
	map<string, int> countmap;
	for (auto &e : fruits)
	{
		countmap[e]++;
	}
   // 排序,先让前k种水果进入队列,然后大于k的在再次比较他们的次数
	priority_queue < pair<string, int>, vector<pair<string, int>, compare>> pq;
	size_t i = 0;
	for (auto e : countmap)
	{
		if (i < k)
		{
			pq.push(e);
			++i;
		}
		else
		{
			if (e.second > pq.top().second)
			{
				pq.pop();
				pq.push(e);
			}
		}
	}
	
}
Published 33 original articles · won praise 13 · views 1061

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/104341462