可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've been doing a basic program to find the max, min, median, variance, mode etc. of a vector. Everything went fine until I got to the mode.
The way I see it, I should be able to loop through the vector, and for each number that occurs I increment a key on the map. Finding the key with the highest value would then be the one that occured the most. Comparing to other keys would tell me if it's a single multiple or no mode answer.
Here's the chunk of code that's been causing me so much trouble.
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;
The entire program can be seen here. http://pastebin.com/MzPENmHp
回答1:
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.
回答2:
You can use std::max_element
to find the highest map value (the following code requires 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';
回答3:
Here's a templated function based on Rob's excellent answer above.
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;
});
}
Example:
std::map<char,int> x = { { 'a',1 },{ 'b',2 },{'c',0}};
auto max=get_max(x);
std::cout << max.first << "=>" << max.second << std::endl;
Outputs: b=>2
回答4:
you are almost there: simply add currentMax = it->second;
after maax = it->first;
but using a map to locate the max is overkill: simply scan the vector and store the index where you find higher numbers: very similar to what you already wrote, just simpler.
回答5:
As someone accustomed to using boost libraries, an alternative to using the anonymous function proposed by Rob is the following implementation of std::max_element:
std::map< int, unsigned >::const_iterator found =
std::max_element( map.begin(), map.end(),
( boost::bind(&std::map< int, unsigned >::value_type::second, _1) <
boost::bind(&std::map< int, unsigned >::value_type::second, _2 ) ) );
回答6:
We may reuse key or, value comparator objects as per requirements in place of comparator api, while fetching min/max/ranges over any STL iterator.
http://www.cplusplus.com/reference/map/multimap/key_comp/
http://www.cplusplus.com/reference/map/multimap/value_comp/
==
Example:
// multimap::key_comp
#include <iostream>
#include <map>
int main ()
{
std::multimap<char,int> mymultimap;
std::multimap<char,int>::key_compare mycomp = mymultimap.key_comp();
mymultimap.insert (std::make_pair('a',100));
mymultimap.insert (std::make_pair('b',200));
mymultimap.insert (std::make_pair('b',211));
mymultimap.insert (std::make_pair('c',300));
std::cout << "mymultimap contains:\n";
char highest = mymultimap.rbegin()->first; // key value of last element
std::multimap<char,int>::iterator it = mymultimap.begin();
do {
std::cout << (*it).first << " => " << (*it).second << '\n';
} while ( mycomp((*it++).first, highest) );
std::cout << '\n';
return 0;
}
Output:
mymultimap contains:
a => 100
b => 200
b => 211
c => 300
==
回答7:
We can easily do this by using max_element() function.
Code Snippet :
#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;
}
回答8:
Beter use inner comparator map::value_comp().
For example:
#include <algorithm>
...
auto max = std::max_element(freq.begin(), freq.end(), freq.value_comp());
std::cout << max->first << "=>" << max->second << std::endl
will output:
Key => Value