2012-02-21 135 views
20

我一直在做一個基本程序來查找矢量的最大值,最小值,中值,方差,模式等。一切都很順利,直到我進入模式。C++幫助查找地圖中的最大值

我看到它的方式,我應該能夠遍歷矢量,並且對於每個發生的數字,我都會在地圖上增加一個鍵。找到價值最高的鑰匙就是最多的鑰匙。與其他鍵比較會告訴我,如果它是一個單一的多或無模式的答案。

下面是導致我非常麻煩的代碼塊。

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

回答

5

您從未在您的代碼中更改過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; 

找到模式的另一種方法是對矢量進行排序並循環一次,以跟蹤值發生變化的指數。

+0

非常感謝你,非常完美。 – Sh0gun 2012-02-21 01:47:30

+0

對於大型地圖,使用地圖成員函數(可能與二分搜索結合),std :: map :: upper_bound,應該更快一些? – 2016-02-10 14:55:57

2

你幾乎有:只需添加currentMax = it->second;maax = it->first;

,但使用的地圖,找到最大的矯枉過正:只需掃描矢量和商店,你找到更高的數字指標:已經非常相似,你寫道,更簡單。

55

您可以使用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'; 
+0

嗨,羅布,如何理解這個功能?它是運算符[]的重載嗎? (const pair &p1,const pair &p2){ return p1.second thinkhy 2014-07-23 23:14:24

+0

http://en.wikipedia.org/wiki/C%2B%2B11#Lambda_functions_and_expressions http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B_.28since_C.2B.2B11.29 – 2014-10-04 16:55:41

+1

Shouldn 't int <..>中的int是const,即對? – thomasa88 2015-02-05 18:51:48

2

正如有人習慣使用Boost庫,另一種使用由羅布提出的匿名函數是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))); 
-1

Beter使用內部比較器map :: value_comp()。

例如:

#include <algorithm> 
... 
auto max = std::max_element(freq.begin(), freq.end(), freq.value_comp()); 
std::cout << max->first << "=>" << max->second << std::endl 

將輸出:

Key => Value 
+7

下面的代碼將不起作用。 auto p = std :: max_element(freq.begin(),freq.end(),freq.value_comp());由於> std :: map :: value_comp返回一個比較對象,可用於比較兩個元素,以獲取第一個元素的鍵是否在第二個元素的前一個變爲 >。所以p將指向地圖中的最後一個元素。 – ivan2kh 2014-03-03 23:11:06

+2

這是錯誤的比較。見http://www.cplusplus.com/reference/map/map/value_comp/ – mmdanziger 2014-12-04 13:57:13

+0

完全錯誤!請修復或刪除。 – juanchopanza 2015-06-03 05:59:12

1

我們可以重新使用鍵或,值比較對象作爲每代替比較器API的要求,同時取最小/最大/範圍過任何STL迭代器。

http://www.cplusplus.com/reference/map/multimap/key_comp/ http://www.cplusplus.com/reference/map/multimap/value_comp/

==

實施例:

// 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 

==

3

下面是基於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; 
    }); 
} 

實施例:

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

輸出:B => 2

+0

謝謝!對於搜索即用型工作功能的用戶來說,這是一個有用的答案! – AlwaysLearning 2017-01-04 12:42:18

0

我們可以很容易地通過使用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; 
}