#include "iostream"
#include "map"
#include "utility"
#include "sstream"
#include "vector"
using namespace std;
int main(){
std::multimap<int,std::string> 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 <<std::endl;
std::pair<std::multimap<int,std::string>::iterator,std::multimap<int,std::string>::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;
}
auto u = m.upper_bound(7);
std::cout << "u " << u->first << " " << u->second << std::endl;
std::string old(" i am a good man!");
stringstream io(old);
std::string s;
std::vector<string> v;
while (io >> s){
v.push_back(s);
}
std::cout << " v.size " << v.size() << std::endl;
for (auto s : v){
std::cout << " " << s << std::endl;
}
return 0;
}
结果:
andrew@DESKTOP-1CLR4DJ /cygdrive/e/study/map
$ g++ map.cpp -std=c++11
andrew@DESKTOP-1CLR4DJ /cygdrive/e/study/map
$ ./a.exe
0
5
key = 3
3 = i am3-1
key = 7
7 = i am7-1
7 = i am7-2
key = 8
8 = i am8-1
8 = i am8-2
i 0 7 i am7-1
i 1 7 i am7-2
u 8 i am8-1
v.size 5
i
am
a
good
man!