【发布时间】:2021-04-18 01:38:02
【问题描述】:
我想将学生的卷和分数按卷的排序顺序存储在地图中,并检索给定卷的分数。学生信息将存储在班级中。我提到了一个不。过去的堆栈溢出帖子,其中解释了数据成员的映射但并不具体。
以下是我的课程设计,但现在我不知道如何将学生课程与 student_map 课程联系起来,我已经尝试过了,但编译器说不匹配 'operator
#include <iostream>
#include <map>
#include <utility>
#include <algorithm>
#include <cstring>
using namespace std;
class Student
{
int roll;
float score;
public:
Student(int r=0)
{
roll=r;
}
Student(float s=0.0)
{
//roll=r;
score=s;
}
bool operator <(Student s)
{
if(roll<s.roll)
return true;
else
{
return false;
}
}
friend ostream & operator <<(ostream &, Student &);
};
ostream & operator << (ostream & out,Student & s)
{
out<<s.roll<<endl;
out<<s.score<<endl;
return out;
}
class Student_map
{
map<Student,Student>stu;
public:
void insert_data(int n,float f)
{
stu.insert(map<Student,Student>::value_type(n,f));
}
void display()
{
map<Student,Student>::iterator it;
for(it=stu.begin();it!=stu.end();it++)
cout<<(*it).first<<" "<<(*it).second;
}
};
int main()
{
Student_map stm;
stm.insert_data(45,100.00);
stm.insert_data(12,50.7);
stm.insert_data(80,50.9);
stm.display();
}
【问题讨论】:
-
您的解释很难理解,而且您自己似乎并没有完全理解您要编写的程序的要求。唯一能帮助你解决这个问题的人是你的老师或导师,或者给你这个编程任务的人。这里的任何人都不太可能有相同的老师或讲师,相同的作业,并且知道它需要你做什么,以及谁能帮助你澄清要求。
-
@SamVarshavchik 我对代码进行了一些编辑,使其更易于理解
-
我不记得对显示的代码说了什么。我很确定我写的是解释,而不是代码,很难理解。而且我仍然没有相同的老师或讲师,或相同的作业,并且不知道它需要任何人做什么。唯一能帮助你解释你的项目要求的人就是给你这个任务的人。
-
你说你得到了很多错误。请显示这些错误消息。
-
我们缺少信息。但如果有帮助,
stu的类型为map<int, Student>,这意味着它将整数映射到学生。那么map<int, Student>::valueType的类型是pair<const int, Student>。然而,您尝试通过提供(n, f)来构造一个值类型,其中 n 是整数,f 是浮点数。换句话说,您的映射是从整数到学生的,但您将其视为从整数到浮点的映射。如果f是一个学生对象,或者如果一个学生可以从一个浮点数构造,那么它会更有意义。也可能有其他错误;这立即引起了我的注意。