思路

这个和上面的成绩排序其实一样,但是要简单很多,这个就采用重载运算符的做法了。重载运算符可以参考一下我的博客 https://blog.csdn.net/sinat_41895958/article/details/113369584

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

class Student{
public:
    int code;
    int grade;
public:
    Student(int code, int grade):code(code),grade(grade){}
    bool operator <(const Student& stu)const {
        return stu.grade == grade ? code < stu.code : grade < stu.grade;
    }
};

int main(){
    int n;
    while(cin >> n){
        vector<Student> students;
        for(int i = 0; i < n; i ++){
            int code; int grade;
            cin >> code >> grade;
            students.emplace_back(code, grade);
        }
        sort(students.begin(), students.end());
        for(Student stu : students){
            cout << stu.code << " " << stu.grade << endl;
        }
    }
    return 0;
}