#include <bits/stdc++.h>
using namespace std;
/*
map的原型:
template<class Key,class Compare=less<Key>,class Allocator=allocator<pair const Key,T>>>
class map;
map不允许有重复key
multimap允许,且都默认按key从小到大排序
unordered_map不允许重复key,不默认排序,而是用哈希函数将键映射到存储桶中
*/
int main(){
//map定义
map<int,int> mymap;
//插入元素
mymap.insert({3,4});
mymap.insert({2,3});
mymap.insert({2,3});
//计某key的数
cout<<mymap.count(2)<<endl;
//计总数
cout<<mymap.size()<<endl;
//迭代器定义
map<int,int>::iterator it=mymap.begin();
//遍历map
while(it!=mymap.end()){
cout<<(*it).first<<" ";
it++;
}
cout<<endl;
//返回指向第一个大于等于指定键元素的位置
cout<<(*mymap.lower_bound(1)).first<<endl;
//返回指向第一个小于等于指定键元素的位置
cout<<(*(mymap.upper_bound(3))).first<<endl;
//判空
if(!mymap.empty()){
//指定删除
mymap.erase(2);
//清空
mymap.clear();
}
return 0;
}
蓝桥杯备赛笔记---map
最新推荐文章于 2024-06-23 18:22:51 发布