简单排序
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct Student{ string id, name; int grade; Student(string id, string name, int grade) : id(id), name(name), grade(grade){} }; int main(){ int N, C; string id, name; int grade; while(cin >> N >> C){ vector<Student> students; for(int i = 0; i < N; i ++){ cin >> id >> name >> grade; students.emplace_back(id, name, grade); } // 按条件排序 sort(students.begin(), students.end(), [=](Student& stu1, Student& stu2){ if(C == 1) return stu1.id < stu2.id; else if(C == 2) return stu1.name == stu2.name ? stu1.id < stu2.id : stu1.name < stu2.name; else return stu1.grade == stu2.grade ? stu1.id < stu2.id : stu1.grade < stu2.grade; }); cout << "Case:" << endl; for(Student stu : students) cout << stu.id + " " + stu.name + " " + to_string(stu.grade) << endl; } return 0; }