P174 映射的应用 [散列表map]

//P174 映射的应用 [散列表map]
#include <iostream>
#include <cstdio>
#include <map>

using namespace std;


map<string,string> student;


int main(){
    int n;
    scanf("%d",&n);
    getchar();

    for(int i=0;i<n;++i){
        string str;
        getline(cin,str);
        int pos = str.find(" ");
        string key = str.substr(0,pos);
        student[key] = str;
    }

    int m;
    scanf("%d",&m);
    for(int i=0;i<m;++i){
        string key;
        cin>>key;



//        string answer = student[key];
//        if(answer == "")  answer = "No Answer!";
//        cout << answer << endl;


        map<string,string>::iterator it = student.find(key); //方法二 使用迭代器
        if(it != student.end()){
            cout<< it->second<<endl;
        }else{
            cout<<"No Answer!"<<endl;
        }
    }
    return 0;
}

全部评论

相关推荐

04-06 16:59
已编辑
河南工业大学 Java
牛牛牛的牛子:最好扔了,实在没有选择的选择
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务