c++中std::multimap遍历

#include "iostream"
#include "map"
#include "utility"
using namespace std;

int main(){

    std::multimap m;

    std::cout << m.size() << std::endl;

    m.insert({3,"i am3-1"});
    m.insert({7,"i am7-1"});
    m.insert({7,"i am7-2"});
    m.insert({8,"i am8-1"});
    m.insert({8,"i am8-2"});
    std::cout << m.size() << std::endl;

    for (auto it = m.begin(); it != m.end(); it = m.upper_bound(it->first)){
        std::cout <<"key = " << it->first <::iterator,std::multimap::iterator> lu = m.equal_range(it->first);
        for (auto i = lu.first; i != lu.second; i++){
            std::cout << i->first << " = " << i->second << std::endl;
        }
    }

    auto it = m.lower_bound(7);
    int num = m.count(7);
    for (int i = 0; i < num; i++,it++){
        std::cout << "i " << i << " " << it->first << " " << it->second << std::endl;
    }
    return 0;
}

 

你可能感兴趣的:(c++)