【来源】
【分析】
建立student结构,把student数据存入vector,利用STL 的sort函数排序后,依次输出即可。
【代码】
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct student{
string name;
string id;
int grade;
};
bool compare(student lhs, student rhs)
{
return lhs.grade < rhs.grade;
}
int main()
{
int n;
cin >> n;
vector<student> students;
for (int i = 0; i < n; ++i){
student stu;
cin >> stu.name >> stu.id >> stu.grade;
students.push_back(stu);
}
sort(students.begin(), students.end(), compare);
vector<student>::const_iterator begin = students.begin();
vector<student>::const_iterator end = students.end()-1;
cout << end->name << " " << end->id << '\n'
<< begin->name << " " << begin->id << endl;
return 0;
}
【点评】
此题考察简单的排序。