查找学生信息
查找学生信息
http://www.nowcoder.com/questionTerminal/fe8bff0750c8448081759f3ee0d86bb4
1、定义学生类:学号,姓名,性别,年龄
2、重写输入输出函数(io)
3、根据建立学号和学生的对应关系
4、根据学号找学生,可以使用find和count,不要使用中括号查找;
#include <iostream>
#include <map>
using namespace std;
class student
{
public:
string id;
string name;
string gender;
int age;
friend istream& operator>>(istream& in,student& s)
{
in >> s.id >> s.name >> s.gender >> s.age;
return in;
}
friend ostream& operator<<(ostream& out,student s)
{
out << s.id << ' ' << s.name << ' ' << s.gender << ' ' << s.age;
return out;
}
};
int main()
{
int n;
int M;
student s;
string id;
while(cin >> n)
{
map<string,student> m;
for(int i = 0;i<n;i++)
{
cin >> s;
m.insert(make_pair(s.id, s));
}
cin >> M;
for(int i = 0;i<M;i++)
{
cin >> id;
if(m.count(id)) cout << m[id] << endl;
else cout << "No Answer!" << endl;
}
}
}
