2012-02-21 8 views
20

मैं एक वेक्टर के अधिकतम, न्यूनतम, औसत, भिन्नता, मोड इत्यादि को खोजने के लिए एक बुनियादी कार्यक्रम कर रहा हूं। सब कुछ ठीक हो गया जब तक कि मैं मोड में नहीं आया।सी ++ मानचित्र में अधिकतम मूल्य ढूँढने में मदद

जिस तरह से मैं इसे देखता हूं, मुझे वेक्टर के माध्यम से लूप करने में सक्षम होना चाहिए, और प्रत्येक संख्या के लिए मैं मानचित्र पर एक कुंजी बढ़ाता हूं। उच्चतम मूल्य के साथ कुंजी ढूंढना तब सबसे अधिक होता है। अन्य कुंजियों की तुलना में मुझे बताएगा कि यह एक एकल या कोई मोड उत्तर नहीं है।

यहां कोड का हिस्सा है जो मुझे इतना परेशानी पैदा कर रहा है।

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; 

संपूर्ण कार्यक्रम यहां देखा जा सकता है। overkill बस के बाद maax = it->first;

currentMax = it->second; जोड़ते हैं, लेकिन अधिकतम पता लगाने के लिए एक नक्शा उपयोग कर रहा है: 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

+0

एक बड़े मानचित्र के लिए, यह नक्शा सदस्य फ़ंक्शन (शायद बाइनरी खोज के साथ संयुक्त) का उपयोग करने के लिए तेज़ होना चाहिए, std :: map :: upper_bound? –

2

आप लगभग देखते हैं बस वेक्टर स्कैन और सूचकांक जहां अधिक संख्या को खोजने की दुकान: पहले से ही बहुत है कि तुम क्या करने के लिए इसी लिखा, बस आसान है।

55

आप std::max_element उपयोग कर सकते हैं उच्चतम मैप मान खोजने के लिए (निम्नलिखित कोड की आवश्यकता है सी ++ 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

हाय रॉब, फ़ंक्शन को कैसे समझें? क्या यह ऑपरेटर का अधिभार है []? [] (कॉन्स जोड़ी और पी 1, कॉन्स जोड़ी और p2) { वापसी p1.second thinkhy

+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 –

+1

कंधे जोड़ी में int <..> कॉन्स हो, यानी जोड़ी ? – thomasa88

2

किसी बढ़ावा पुस्तकालयों, गुमनाम समारोह रोब द्वारा प्रस्तावित उपयोग का एक विकल्प का उपयोग कर के आदी के रूप में

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 उपयोग आंतरिक तुलनित्र नक्शा :: value_comp(): std :: max_element के निम्नलिखित कार्यान्वयन है।

उदाहरण के लिए:

#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

शामिल है नीचे दिया गया कोड काम नहीं करेगा। ऑटो पी = std :: max_element (freq.begin(), freq.end(), freq.value_comp()); चूंकि> std :: map :: value_comp एक तुलना ऑब्जेक्ट देता है जिसे पर उपयोग किया जा सकता है> दो तत्वों की तुलना करें ताकि यह पता चल सके कि पहले की कुंजी > दूसरे से पहले है या नहीं। तो पी मानचित्र में अंतिम तत्व को इंगित करेगा। – ivan2kh

+2

यह गलत तुलनित्र है। देखें http://www.cplusplus.com/reference/map/map/value_comp/ – mmdanziger

+0

पूरी तरह से गलत! कृपया ठीक करें या हटाएं। – juanchopanza

1

हम तुलनित्र एपीआई के स्थान पर आवश्यकताओं के अनुसार कुंजी का पुन: उपयोग कर सकते हैं या, मूल्य तुलनित्र वस्तुओं न्यूनतम/अधिकतम लाते समय/अधिक पर्वतमाला कोई एसटीएल इटरेटर।

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

यहाँ एक टेम्प्लेटेड रोब उत्तम जवाब ऊपर के आधार पर सुविधा नहीं होती।

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; 

आउटपुट: ख => 2

+0

धन्यवाद! यह एक ऐसा उत्तर है जो उन लोगों के लिए उपयोगी है जो उपयोग में आसान काम करने के लिए खोज करते हैं! – AlwaysLearning

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; 
} 
संबंधित मुद्दे