而是订购了std :: tr1 :: unordered_map

时间:2015-11-04 19:30:57

标签: c++ unordered-map c++03 tr1

我想要一个包含每个键出现次数的unordered_map;问题是,当我打印myMap时,它会被订购。我知道使用C ++ 11会更好,但由于某些原因我不能解释。

#include <iostream>
#include <tr1/unordered_map>

using namespace std;
using std::tr1::unordered_map;

main () {
    unordered_map<int,int> myMap;
    int counter, key;

    cin>>counter;
    for (int i=0; i<counter; i++) {
        cin>>key; // Read key from keyboard
        if (myMap.find(key) == myMap.end()) // If key is not already present in myMap
            myMap[key] = 1; // Insert the new key with 1 occurrence
        else
            myMap[key]++; // Increment the current key occurrence
    }
    for (unordered_map<int,int>::const_iterator it = myMap.begin(); it != myMap.end(); ++it)
        cout << "[" << it->first << "," << it->second << "] " << endl;
}

0 个答案:

没有答案