llllmz

导航

KY27 查找学生信息C++

用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;
}

结果如下:

posted on 2024-01-30 16:51  神奇的萝卜丝  阅读(20)  评论(0)    收藏  举报