map容器
// TODO map容器
#include <iostream>
#include <map>
using namespace std;
int main() {
// TODO map<int, string>按key值排序,同一个key不可以重复插入
map<int, string> map1;
map1.insert(pair<int, string>(1, "111"));
map1.insert(pair<int, string>(3, "333"));
map1.insert(pair<int, string>(2, "222"));
map1.insert(pair<int, string>(2, "2222"));
// 修改key对应的 value
map1[3] = "3333";
for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++) {
cout << "key " << it->first << ",value " << it->second << endl;
}
cout << endl;
// TODO multimap<int, string>按key值排序,可以重复插入同的key,使用场景--分组
multimap<int, string> multimap1;
multimap1.insert(pair<int, string>(1, "111"));
multimap1.insert(pair<int, string>(3, "333"));
multimap1.insert(pair<int, string>(2, "222"));
multimap1.insert(pair<int, string>(2, "2222"));
multimap1.insert(pair<int, string>(2, "22"));
for (auto it = multimap1.begin(); it != multimap1.end(); it++) {
cout << "key " << it->first << ",value " << it->second << endl;
}
int key;
cout << "请输入要查找的key:" << endl;
cin >> key;
multimap<int, string>::iterator it = multimap1.find(key);
for (; it != multimap1.end(); it++) {
if (it->first != key) {
break;
}
cout << "key " << it->first << ",value " << it->second << endl;
}
return 0;
}