2013-07-15 4 views
2

나는 우리가 배치 한 모든 값을 볼 수 있는지보고 싶습니다. 예를 들어 :정렬되지 않은 멀티 맵은 모든 값을 찾습니다.

#include <iostream> 
#include <unordered_map> 

using namespace std; 


int main() { 
    unordered_multimap<string,int> hash; 

    hash.emplace("Hello", 12); 
    hash.emplace("World", 22); 
    hash.emplace("Wofh", 25); 
    for (int i = 1; i < 10; i++) { 
     hash.emplace("Wofh", i); 
    } 
    cout << "Hello " << hash.find("Hello")->second << endl; 
    cout << "Wofh " << hash.count("Wofh") << endl; 
    cout << "Wofh " << hash.find("Wofh")->second << endl; 

    return 0; 
} 

출력은 다음과 같습니다

$ ./stlhash 
Hello 12 
Wofh 10 
Wofh 9 

나는 마지막 줄은 분명히 findfirstsecond 포인터 등의 소요 (9)에 ... 25,1,2에서 보여주고 싶은 반면 첫 번째는 값이고 두 번째는 해당 값입니다. 이 일을 할 수있는 방법이 있습니까?

+0

가능한 중복 http://stackoverflow.com/questions/9046922/ c-unordered-multimap) – nimcap

답변

2

당신이 cplusplus.com에서 equal_range

예라고해야 할 작업 :

// 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; 
} 
[C++ 정렬되지 않은 \의 _multimap] (의