简体   繁体   English

从地图获取所有值

[英]Getting all the values from a map

I have a class Student which contains a name, a private int regNo and a private map which is a store of their marks. 我有一个Student类,其中包含一个名称,一个私有的int regNo和一个存储其标记的私有地图。 This is the constructor. 这是构造函数。

Student::Student (string const& name, int regNo):Person(name), regNo(regNo)
{
    map<string, float> marks;
}

I need to write a function that takes two parameters a collection of students mine are stored in a vector, and a float that the user provides, the function should output the name of the student, and the min, max and average marks when their average is greater than a user provided input. 我需要编写一个函数,该函数带有两个参数,一个向量集存储着我的一个学生集合,一个用户提供的浮点数,该函数应该输出学生的名字,以及在平均时的最小,最大和平均分数大于用户提供的输入。 My problem is what's the easiest way to get all the values (marks) out of the map? 我的问题是从地图中获取所有值(标记)的最简单方法是什么? as in accessing the map and getting all of the marks, do I need a function in the student class that returns a mark, how's best to do that? 就像访问地图并获得所有标记一样,我是否需要在学生班级中提供一个返回标记的函数,如何最好地做到这一点? Thanks. 谢谢。

You can create a function that returns a vector of all the marks 您可以创建一个返回所有标记的向量的函数

vector<string> vec;
for( map<string,float>::iterator it = marks.begin(); it != marks.end(); ++it) 
{
    vec.push_back(it->first);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM