map<String,student>怎么获取student的数据
时间: 2024-03-01 13:51:32 浏览: 19
要获取`map<String, student>`中的`student`数据,需要使用`std::map`的迭代器进行遍历,如下所示:
```c++
#include <iostream>
#include <map>
#include <string>
struct student {
int age;
std::string gender;
};
int main() {
std::map<std::string, student> my_map;
my_map.insert({"Tom", {20, "male"}});
my_map.insert({"Lucy", {21, "female"}});
my_map.insert({"Jack", {19, "male"}});
// 遍历map中的每一个元素
for (auto it = my_map.begin(); it != my_map.end(); ++it) {
std::cout << "Key: " << it->first << std::endl;
std::cout << "Value: " << it->second.age << ", " << it->second.gender << std::endl;
}
return 0;
}
```
在上述代码中,我们定义了一个`map`对象`my_map`,并向其中插入了三个`<string, student>`对。接着,使用迭代器遍历`my_map`中的每一个元素,并输出其中的`student`数据。