优先队列的实例题
显示学生信息(学号、姓名、语文、数学),条件是:总成绩由高到低,当总成绩相同时,语文成绩高者优先。
#include<iostream>
#include<queue>
#include<string>
using namespace std;
class Student
{
int No;
string name;
int chinese;
int math;
public:
Student(int s_no,string s_name,int s_chinese,int s_math):
No(s_no),name(s_name),chinese(s_chinese),math(s_math)
{
}
int GetNo()
{
return No;
}
string GetName()
{
return name;
}
int GetChinese()
{
return chinese;
}
int GetMath()
{
return math;
}
bool operator<(const Student& s)const
{
int sum1=chinese+math;
int chinese1=s.chinese;
int math1=s.math;
int sum2=chinese1+math1;
if(sum1<sum2) return true;
if((sum1==sum2)&&(chinese<chinese1)) return true;
return false;
}
};
void main()
{
Student s[]={
Student(1001,"zhang",70,80),
Student(1002,"li",80,70),
Student(1003,"wang",90,85),
Student(1004,"zhao",85,75)
};
priority_queue<Student>pr(s,s+4);
cout<<"成绩由高到低(当相同时,语文高优先):"<<endl;
cout<<"学号\t姓名\t语文\t数学"<<endl;
while(!pr.empty())
{
Student& t=pr.top();
cout<<t.GetNo()<<"\t"<<t.GetName()<<"\t"<<t.GetChinese()<<"\t"<<t.GetMath()<<endl;
pr.pop();
}
}