用map做查找就行了。
#include<iostream>
#include<string>
#include<map>
using namespace std;
struct node{
string name;
string x;
int age;
};
typedef struct node sinfo;
int main() {
int n;
while(cin >> n){
map<string,sinfo> m;
while(n!=0){//建立map
string num;
sinfo tem;
cin >> num >> tem.name >> tem.x >> tem.age;
m[num]=tem;
n--;
}
int y;
cin >> y;
while(y!=0){
string x;
cin >> x;
map<string,sinfo> :: iterator it =m.find(x);
if(it!=m.end()){
cout << it->first <<' ' << it->second.name << ' ' << it->second.x <<' ' << it->second.age <<'\n';
}else{
cout << "No Answer!" << '\n';
}
y--;
}
}
return 0;
}
结果如下:
标签:map,tem,int,cin,C++,second,查找,KY27,string From: https://www.cnblogs.com/llllmz/p/17997465