我一直在做一個基本程序來查找矢量的最大值,最小值,中值,方差,模式等。一切都很順利,直到我進入模式。C++幫助查找地圖中的最大值
我看到它的方式,我應該能夠遍歷矢量,並且對於每個發生的數字,我都會在地圖上增加一個鍵。找到價值最高的鑰匙就是最多的鑰匙。與其他鍵比較會告訴我,如果它是一個單一的多或無模式的答案。
下面是導致我非常麻煩的代碼塊。
map<int,unsigned> frequencyCount;
// This is my attempt to increment the values
// of the map everytime one of the same numebers
for(size_t i = 0; i < v.size(); ++i)
frequencyCount[v[i]]++;
unsigned currentMax = 0;
unsigned checked = 0;
unsigned maax = 0;
for(auto it = frequencyCount.cbegin(); it != frequencyCount.cend(); ++it)
//checked = it->second;
if (it ->second > currentMax)
{
maax = it->first;
}
//if(it ->second > currentMax){
//v = it->first
cout << " The highest value within the map is: " << maax << endl;
整個程序可以在這裏看到。 http://pastebin.com/MzPENmHp
非常感謝你,非常完美。 – Sh0gun 2012-02-21 01:47:30
對於大型地圖,使用地圖成員函數(可能與二分搜索結合),std :: map :: upper_bound,應該更快一些? – 2016-02-10 14:55:57