点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin>>n;
unordered_map<string,int> scores;
int score;
string name;
while(n--){
int op;
cin>>op;
if(op==1){
cin>>name>>score;
scores[name]=score;
cout<<"OK"<<endl;
}else if(op==2){
cin>>name;
auto it=scores.find(name);
if(it==scores.end()) cout<<"Not found"<<endl;
else cout<<it->second<<endl;
}else if(op==3){
cin>>name;
if(scores.erase(name))cout<<"Deleted successfully"<<endl;
else cout<<"Not found"<<endl;
}else if(op==4) cout<<scores.size()<<endl;
}
return 0;
}
简简单单的哈希map,真是方便呢,利用数据结构轻松实现各个操作。核心操作是,1.[]访问,或创建赋值2.find()查找,成功返回指针(整体键对),失败返回end()3清除erase(),成功可通过if,失败无法通过4.只能通过键访问值,不能通过值访问键