map映射的特点
1.键值映射(key-val)映射
用key去映射val
2.默认去重加升序
map<key,val>
3.less升序,greater降序
map<key,val,less>
map<key,val,greater>
map<string,int,less<string>> mp={
{"3.斗罗大陆",50},
{"2.逆天邪神",25},
{"1.斗破苍穹",60}
};
for(auto it:mp){
cout<<it.first<<" "<<it.second<<endl;
}
map<string,int,greater<string>> mp1{
{"1.斗罗大陆",50},
{"2.逆天邪神",25},
{"3.斗破苍穹",60}
};
for(auto it:mp1){
cout<<it.first<<" "<<it.second<<endl;
}
内置函数
1.查找mp.find(key)
if(mp.find(s)!=mp.end()) cout<<"find!"<<endl;
else cout<<"no find!"<<endl;
2.插入mp.insert(key)
insert插入还有下标插入
// mp.insert({"4.陈泽",100});
mp["4.陈泽"]=100;
for(auto it:mp){
cout<<it.first<<" "<<it.second<<endl;
}
3.【 】的用法
可以插入元素
还可以查询用
string s;cin>>s;
if(mp[s]!=0) cout<<"yes"<<endl;
else cout<<"no"<<endl;