读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。
备注:本题出自http://pat.zju.edu.cn/contests/pat-b-practise/1004
分析:这是一道简单的排序题,排序的对象为学生,排序的判别准则为学生的成绩。
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct Student
{
string m_name;
string m_number;
int m_score;
friend istream& operator>>(istream& is,Student& stu);
friend ostream& operator<<(ostream& os,Student& stu);
};
istream& operator>>(istream& is,Student& stu)
{
is>>stu.m_name>>stu.m_number>>stu.m_score;
return is;
}
ostream& operator<<(ostream& os,Student& stu)
{
os<<stu.m_name<<" "<<stu.m_number;
return os;
}
bool score_compare(const Student& lhs,const Student& rhs)
{
return lhs.m_score<rhs.m_score;
}
int main()
{
int student_number;
cin>>student_number;
if (student_number<=0)
return -1;
vector<Student> stu(student_number);
for(int i=0;i<student_number;++i)
cin>>stu[i];
sort(stu.begin(),stu.end(),score_compare);
cout<<stu[student_number-1]<<endl;
cout<<stu[0]<<endl;
}