#include <bits/stdc++.h>
using namespace std;
map<int, int> mp; //按键小到大排序
int main(){
// 1.插入键值对
mp[2] = 2; // (原先存在则无法插入)
mp.emplace(1, 1); // (原先存在也可插入)
// 2.删除键值对
mp.erase(2); // 删除键为2的键值对
// 3.查询键值对是否存在
if(mp.find(2) == mp.end())cout << "not have k=2" << '\n';
if(!mp.count(2))cout << "not have k=2" << '\n';
// 4.统计map键值对个数
cout << mp.size() << '\n';
// 5.遍历
for(auto i: mp){
cout << i.first << " " << i.second << '\n';
}
// 6.清空map
mp.clear();
return 0;
}
STL容器:map的常用操作
于 2024-11-22 21:00:28 首次发布