映射
#include<iostream> #include<string> #include<map> using namespace std; int main(){ map<string,int>dict; dict["Jone"]=1; dict["Tom"]=2; dict["Mary"]=1; if(dict.count("Mary")){ count<<"Mary is in class"<<dict["Mary"]; dict["Mary"]=5; } for(map<string,int>:: iterator it=dict.begin();it!=dict.end();it++){ cout>>it->first<<"is in class"it->second<<endl; } dict.clear(); return 0; }
二维映射
#include<iostream> #include<map> #include<string> using namespace std; int main(){ map<int,mao<string,int> >info; int n; cin >>n; for(int i=0;i<n;i++){ int class_id; string name; cin>>class_id>>name; info[class_id][name]++; } for(map<int,map<string,int> >::iterator it=info.begin();it1!=info.end();it1++){ for(map<string,int>::iterator it2=it1->second.begin();it2!=it1->second.end();it2++){ cout<<"There are"<<it2->second<<"people named" it2->first<<"in class"<<it1->first<<endl } } }