2012-01-28 6 views
10

सी ++ में मल्टीमैप वास्तव में अजीब काम करने के लिए लगता है, मैं चाहते हैं पता करने के लिए क्योंunordered_multimap - ढूंढें का परिणाम (पुनरावृत्ति) भिन्न मान के साथ तत्वों पैदावार

#include <iostream> 
#include <unordered_map> 

using namespace std; 

typedef unordered_multimap<char,int> MyMap; 

int main(int argc, char **argv) 
{ 
    MyMap map; 
    map.insert(MyMap::value_type('a', 1)); 
    map.insert(MyMap::value_type('b', 2)); 
    map.insert(MyMap::value_type('c', 3)); 
    map.insert(MyMap::value_type('d', 4)); 
    map.insert(MyMap::value_type('a', 7)); 
    map.insert(MyMap::value_type('b', 18)); 

    for(auto it = map.begin(); it != map.end(); it++) { 
     cout << it->first << '\t'; 
     cout << it->second << endl; 
    } 

    cout << "all values to a" << endl; 
    for(auto it = map.find('a'); it != map.end(); it++) { 
     cout << it->first << '\t' << it->second << endl; 
    } 

} 

इस उत्पादन है:

c 3 
d 4 
a 1 
a 7 
b 2 
b 18 
all values to a 
a 1 
a 7 
b 2 
b 18 

जब भी मैं स्पष्ट रूप से 'ए' मांग रहा हूं तो आउटपुट में अभी भी कुंजी के साथ कुछ भी क्यों शामिल है? क्या यह एक कंपाइलर या एसएलएल बग है?

उत्तर

36

find, लागू होने के रूप में, पहले तत्व के लिए एक पुनरावर्तक देता है जो मल्टीमैप (किसी अन्य मानचित्र के साथ) में कुंजी से मेल खाता है। आप की संभावना equal_range लिए देख रहे हैं:

// Finds a range containing all elements whose key is k. 
// pair<iterator, iterator> equal_range(const key_type& k) 
auto its = map.equal_range('a'); 
for (auto it = its.first; it != its.second; ++it) { 
    cout << it->first << '\t' << it->second << endl; 
} 
+0

अगर आप बदलना -> करने के लिए। तो मैं आपका जवाब स्वीकार करूंगा। – Arne

+1

मुझे '-> 'खुश मिला। – user7116

-1

यह प्रतीत होता है कि आप पूरी जोड़े की "सूची" में एक इटरेटर मिलता है, इसके साथ कुंजी है 'एक' के रूप में पहली जोड़ी पर शुरू। तो जब आप अंत तक फिर से जाते हैं, तो स्वाभाविक रूप से आपको 'ए' से परे सब कुछ मिल जाएगा। यदि आपने 'सी' की मांग की है, तो आप संभवतः पूरे "सूची" के माध्यम से फिर से काम करेंगे जो आप वहां करते हैं। शायद आपको इसे "it! = Map.end() & & यह-> पहला == 'ए'" अगर आप सभी को चाहते हैं तो इसे पुन: प्रयास करना चाहिए।

8

यह एक बग नहीं है, यह डिज़ाइन द्वारा है। find मेल खाने वाले तत्वों में से एक को पुनरावर्तक देता है, बस इतना ही। आप अपने निर्माण के साथ मानचित्र के अंत में पुन: प्रयास करेंगे।

आपको जो भी हो रहा है उसके लिए आपको multimap::equal_range का उपयोग करने की आवश्यकता है।

4

www.cplusplus.com में एक उदाहरण है, समान तत्व वाले सभी तत्वों को प्राप्त करने के लिए बराबर_रेेंज विधि का उपयोग कैसे करें।

// unordered_multimap::equal_range 
#include <iostream> 
#include <string> 
#include <unordered_map> 
#include <algorithm> 

typedef std::unordered_multimap<std::string,std::string> stringmap; 

int main() 
{ 
    stringmap myumm = { 
    {"orange","FL"}, 
    {"strawberry","LA"}, 
    {"strawberry","OK"}, 
    {"pumpkin","NH"} 
    }; 

    std::cout << "Entries with strawberry:"; 
    auto range = myumm.equal_range("strawberry"); 
    for_each (
    range.first, 
    range.second, 
    [](stringmap::value_type& x){std::cout << " " << x.second;} 
); 

    return 0; 
} 

लिंक का संदर्भ लें: http://www.cplusplus.com/reference/unordered_map/unordered_multimap/equal_range/

+0

@einpoklum यह ** उत्तर देने से पहले ** उत्तर है ** कृपया इस मेटा को पढ़ें [आप कर रहे हैं-गलत-एक-याचिका-के-से-कम-गुणवत्ता-पोस्ट-कतार] (http://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue) –

+0

मुझे इसे दोबारा दोहराएं (समीक्षा तंत्र नहीं करता है ' टी आपको विशिष्ट टिप्पणियां करने देता है और आप किसी सूची से चयन कर रहे हैं): ओपी ने पूछा "एक्स क्यों होता है?" - आपका उत्तर, सामान्य रूप से सामान्य रूप से उपयोगी होने पर, ओपी के कोड के साथ क्या होता है इसका स्पष्टीकरण नहीं है। तो, एक जवाब नहीं है। – einpoklum

संबंधित मुद्दे