std::multimap 按照key遍历---

#include <iostream>
#include <unordered_map>

using namespace std;

int main()
{
    unordered_multimap<int, int> umap;
    umap.insert({ 1, 1 });

    umap.insert({ 2, 2 });
    umap.insert({ 2, 1 });

    umap.insert({ 3, 3 });
    umap.insert({ 3, 1 });
    umap.insert({ 3, 2 });
    for(auto it = umap.begin(); it != umap.end(); it = umap.upper_bound(it->first))
    {
        auto range = umap.equal_range(it->first);
        cout << it->first << ":" << endl;
        while(range.first != range.second)
        {
            cout << "   " << range.first->second << endl;
            ++range.first;
        }
    }
    getchar();
    return 0;
}

运行结果:

 

posted @ 2017-10-31 11:19  你好阿汤哥  Views(1975)  Comments(0Edit  收藏  举报