Misha and Changing Handles

关于map函数的一些巧妙运动

 1 #include<iostream>
 2 #include<map>
 3 using namespace std;
 4 int main(){
 5     string a,b;
 6     map<string,string> ma;
 7     int n; 
 8     cin>>n;
 9     for(int i=0;i<n;i++){
10         cin>>a>>b;
11         cout<<ma.count(a)<<endl;
12         // count()返回指定元素出现的次数,即查找a出现的次数 
13         if(!ma.count(a)){
14             ma[b]=a;        
15         }
16         else{
17             ma[b]=ma[a];
18             //erase() 删除改键以及改键对应的元素 
19             ma.erase(a);
20         }
21     }
22     cout<<ma.size()<<endl;
23     map<string,string>::iterator it;
24     //begin() 返回指向map头部的迭代器;end() 返回指向map末尾的迭代器 
25     for(it=ma.begin();it!=ma.end();it++){
26         cout<<it->second<<" "<<it->first<<endl;
27     }
28     return 0;
29 } 
30 /*
31 5
32 Misha ILoveCodeforces
33 Vasya Petrov
34 Petrov VasyaPetrov123
35 ILoveCodeforces MikeMirzayanov
36 Petya Ivanov
37 */
原文地址:https://www.cnblogs.com/0211ji/p/13680330.html