map
#include <iostream>
#include <string.h>
#include <map>
using namespace std;
int main() {
pair<int, string> t(9, "Asia");
//cout << t.first << " " << t.second << endl << endl;
pair<int, string> sz[4] = {
pair<int,string>(9,"USA"),
pair<int,string>(4,"UK"),
pair<int,string>(5,"China"),
pair<int,string>(8,"Germany"),
};
map<int, string> obM(sz, sz + 4);
cout<<"size of map: "<<obM.size()<<endl;
map<int, string>::iterator it = obM.begin();
while (it != obM.end()) {
cout << (*it).first << " "<<(*it).second<<endl;
++it;
}
return 0;
}
map 按关键字大小排序输出
map输出
multimap
#include <iostream>
#include <string.h>
#include <map>
using namespace std;
int main() {
pair<int, string> t(9, "Asia");
//cout << t.first << " " << t.second << endl << endl;
pair<int, string> sz[4] = {
pair<int,string>(9,"USA"),
pair<int,string>(4,"UK"),
pair<int,string>(4,"China"),
pair<int,string>(4,"Germany"),
};
multimap<int, string> obM(sz, sz + 4);
cout<<"size of map: "<<obM.size()<<endl;
multimap<int, string>::iterator it = obM.begin();
while (it != obM.end()) {
cout << (*it).first << " "<<(*it).second<<endl;
++it;
}
return 0;
}
multimap 输出
- multimap 允许关键值重复输出类容