【发布时间】:2012-03-11 08:57:07
【问题描述】:
我一直在做一个基本程序来查找向量的最大值、最小值、中值、方差、众数等。在我进入模式之前一切都很好。
按照我的看法,我应该能够循环遍历向量,并且对于出现的每个数字,我都会在地图上增加一个键。找到具有最高值的键将是出现次数最多的键。与其他键比较会告诉我它是单个多重还是无模式答案。
这是给我带来很多麻烦的代码块。
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
【问题讨论】:
标签: c++ dictionary vector max mode