C++帮助在地图中查找最大值

Sh0*_*gun 38 c++ dictionary vector mode max

我一直在做基本程序来找到矢量的最大值,最小值,中值,方差,模式等.一切都很顺利,直到我进入模式.

我看到它的方式,我应该能够循环遍历向量,对于每个出现的数字,我在地图上增加一个键.找到具有最高值的密钥将是发生最多的密钥.与其他键相比,它会告诉我它是单个多重模式还是无模式答案.

这是导致我如此麻烦的代码块.

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;
Run Code Online (Sandbox Code Playgroud)

整个程序可以在这里看到.http://pastebin.com/MzPENmHp

Rob*_*obᵩ 90

您可以使用它std::max_element来查找最高的映射值(以下代码需要C++ 11):

std::map<int, size_t> frequencyCount;
using pair_type = decltype(frequencyCount)::value_type;

for (auto i : v)
    frequencyCount[i]++;

auto pr = std::max_element
(
    std::begin(frequencyCount), std::end(frequencyCount),
    [] (const pair_type & p1, const pair_type & p2) {
        return p1.second < p2.second;
    }
);
std::cout << "A mode of the vector: " << pr->first << '\n';
Run Code Online (Sandbox Code Playgroud)


cos*_*rgi 18

你们写得太多了。这可以在几行中完成,这是一个完整的工作片段:

#include <iostream>
#include <algorithm>
#include <map>
int main() {
    std::map<char,int> x = { { 'a',1 },{ 'b',2 },{'c',0} };
    std::map<char,int>::iterator best
        = std::max_element(x.begin(),x.end(),[] (const std::pair<char,int>& a, const std::pair<char,int>& b)->bool{ return a.second < b.second; } );
    std::cout << best->first << " , " << best->second << "\n";
}
Run Code Online (Sandbox Code Playgroud)

  • 以避免命名空间污染。污染可能是大型项目的真正障碍。甚至由于链接器混淆来自不同命名空间的相同类名而崩溃:https://gitlab.com/yade-dev/trunk/-/issues/57 (4认同)
  • 为什么每个人都使用 std:: 而不是使用命名空间? (3认同)

YXD*_*YXD 13

currentMax的代码从未改变过.

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;
Run Code Online (Sandbox Code Playgroud)

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

  • 对于大地图,使用地图成员函数(可能结合二分查找),std::map::upper_bound? (2认同)

dak*_*les 12

这是一个基于Rob上面的优秀答案的模板化函数.

template<typename KeyType, typename ValueType> 
std::pair<KeyType,ValueType> get_max( const std::map<KeyType,ValueType>& x ) {
  using pairtype=std::pair<KeyType,ValueType>; 
  return *std::max_element(x.begin(), x.end(), [] (const pairtype & p1, const pairtype & p2) {
        return p1.second < p2.second;
  }); 
}
Run Code Online (Sandbox Code Playgroud)

例:

std::map<char,int> x = { { 'a',1 },{ 'b',2 },{'c',0}}; 
auto max=get_max(x);
std::cout << max.first << "=>" << max.second << std::endl; 
Run Code Online (Sandbox Code Playgroud)

输出:b => 2


ras*_*dcs 6

我们可以通过使用 max_element() 函数轻松地做到这一点。

代码片段:


#include <bits/stdc++.h>
using namespace std;

bool compare(const pair<int, int>&a, const pair<int, int>&b)
{
   return a.second<b.second;
}

int main(int argc, char const *argv[])
{
   int n, key, maxn;
   map<int,int> mp;

   cin>>n;

   for (int i=0; i<n; i++)
   {
     cin>>key;
     mp[key]++;
   }

   maxn = max_element(mp.begin(), mp.end(), compare)->second;

   cout<<maxn<<endl;

   return 0;
 }
Run Code Online (Sandbox Code Playgroud)