构建一个map,还是查找问题。
麻烦点就是要 分解输入的过程
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main(){
string a,b;
map<string,string> m;
while(getline(cin,a)){//构建map
b.clear();
if(a[0]=='@') break;
int i=0;
for(;i<a.size();i++){
if(a[i]==']') {
break;
}
}
i=i+2;
for(int j=i;j<a.size();j++){
b.push_back(a[j]);
}
a.erase(i-1,a.size()-i+1);
m[a]=b;
}
int n;
cin >> n;
char c;
c=getchar();//从输入缓冲区中读入换行
while(n!=0){
string tem;
getline(cin,tem);
map<string,string> :: iterator it =m.begin();
while(it!=m.end()){
if(tem[0]=='['){
if(tem==it->first){
cout << it->second <<'\n';
break;
}
}else{
if(tem==it->second){
string p=it->first;
p.erase(0,1);
p.erase(p.size()-1,1);
cout << p <<'\n';
break;
}
}
it++;
}
if(it==m.end()) cout << "what?"<<'\n';
n--;
}
return 0;
}
结果:
标签:map,tem,int,魔咒,KY146,while,C++,include,string From: https://www.cnblogs.com/llllmz/p/17997854