且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在地图中寻找最大值

更新时间:2023-02-04 08:39:23

您从未更改代码中的 currentMax.

You never changed currentMax in your code.

map<int,unsigned> frequencyCount;
for(size_t i = 0; i < v.size(); ++i)
    frequencyCount[v[i]]++;

unsigned currentMax = 0;
unsigned arg_max = 0;
for(auto it = frequencyCount.cbegin(); it != frequencyCount.cend(); ++it ) }
    if (it ->second > currentMax) {
        arg_max = it->first;
        currentMax = it->second;
    }
}
cout << "Value " << arg_max << " occurs " << currentMax << " times " << endl;

另一种找到模式的方法是对向量​​进行排序并循环遍历一次,跟踪值发生变化的索引.

Another way to find the mode is to sort the vector and loop through it once, keeping track of the indices where the values change.