把学生姓名,成绩录入map,定义student类
时间: 2023-07-04 15:07:10 浏览: 56
好的,以下是示例代码:
```c++
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Student {
public:
string name;
int score;
};
int main() {
map<string, Student> students; // 定义一个map,key为学生姓名,value为Student对象
// 录入学生信息
Student s1 = {"Tom", 80};
students["Tom"] = s1;
Student s2 = {"Jerry", 90};
students["Jerry"] = s2;
// 遍历map,输出学生信息
for (auto it = students.begin(); it != students.end(); ++it) {
cout << "Name: " << it->second.name << ", Score: " << it->second.score << endl;
}
return 0;
}
```
上述代码中,我们定义了一个`Student`类,其中包含学生姓名和成绩两个成员变量。然后我们定义了一个`map`,将学生姓名作为key,将`Student`对象作为value。接着我们将两个学生的信息录入到`map`中,并遍历`map`输出每个学生的信息。