这个题目就是为了练习map,因为有可能从key查找value,由value查找key两个情况,所以需要两个map,因为map只能从key查找value。同时也考验了字符串的处理。
#include<iostream>
#include<map>
#include<string>
#include<cstdio>
using namespace std;
int main(){
map<string,string>maps;
map<string,string>maps1;
map<string,string>::iterator f;
string a;
int n;
while(getline(cin,a)){
if(a=="@END@")
break;
int end = a.find(']');
string tmp = a.substr(1,end-1);
string tmp1 = a.substr(end+2,a.length()-1);
maps[tmp]=tmp1;
maps1[tmp1]=tmp;
}
cin>>n;
getchar();
for(int i=0;i<n;i++){
getline(cin,a);
if(a.find('[')!=-1){
a = a.substr(1,a.length()-2);
f = maps.find(a);
if(f==maps.end())
cout<<"what?"<<endl;
else
cout<<maps[a]<<endl;
}
else{
f = maps1.find(a);
if(f==maps1.end())
cout<<"what?"<<endl;
else
cout<<maps1[a]<<endl;
}
}
return 0;
}原文:http://blog.csdn.net/u010006643/article/details/46551791