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)
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)
找到该模式的另一种方法是对向量进行排序并循环一次,跟踪值变化的索引.
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
我们可以通过使用 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)