题解 | #查找学生信息#

查找学生信息

http://www.nowcoder.com/practice/fe8bff0750c8448081759f3ee0d86bb4

用STL自带的哈希存

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

struct Student
{
    string name;
    string sex;
    int age;
};

unordered_map<string, Student> hashTable;

int main()
{
    int n, m;
    string index;
    Student tmp;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> index;
        cin >> tmp.name >> tmp.sex >> tmp.age;
        hashTable[index] = tmp;
    }
    cin >> m;
    for (int i = 0 ; i < m; ++i)
    {
        cin >> index;
        if (hashTable.count(index))
             cout << index << " "
                  << hashTable[index].name << " "
                  << hashTable[index].sex << " "
                  << hashTable[index].age << endl;   
        else
            cout << "No Answer!" << endl;
    }
    return 0;
}
全部评论

相关推荐

牛客868257804号:九个中铁八个中建
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务