multimap用法

来源:互联网 发布:百度bae域名404 编辑:程序博客网 时间:2024/06/05 08:29
#include <map>
#include <numeric>
#include <string>

// test multimap
    multimap<string, string> a_multimap;
    a_multimap.insert(make_pair("guopeng", "C++"));
    a_multimap.insert(make_pair("guopeng", "algorithm"));
    a_multimap.insert(make_pair("guopeng", "SU code"));

    // method 1
    typedef multimap<string, string>::size_type own_type;
    own_type nums = a_multimap.count("guopeng");
    multimap<string, string>::iterator mul_iter = a_multimap.find("guopeng");
    for (own_type cnt = 0; cnt < nums; cnt++, mul_iter++) {
        cout << "nums of guopeng: " << mul_iter->second << endl;
    }
 
    // mathod 2
    typedef multimap<string, string>::iterator own_iter_type;
    pair<own_iter_type, own_iter_type> pos = a_multimap.equal_range("guopeng");
    while (pos.first != pos.second) {
        cout << "guopeng: " << pos.first->second << endl;
        pos.first++;
    }
原创粉丝点击