不知道sort为啥会报错qnq

P5740 【深基7.例9】最厉害的学生

cyz_luogu @ 2021-12-17 10:22:08


#include<stdio.h>
#include <algorithm>
using namespace std;
#include<iostream>
using namespace std;
bool cmp(students x, students y) {
    if (x.score == y.score)return x.id < y.id;
    return x.score > y.score;
}
struct students {
    string name;
    int Chinese, Maths, English, score,id;
};
students a[1001];
int main() {
    int n,maxx=0,w;
    cin >> n;
    for (int i=0; i < n; i++) {
        cin >> a[i].name >> a[i].Chinese >> a[i].Maths >> a[i].English ;
        a[i].score = a[i].Chinese + a[i].Maths + a[i].English;
        a[i].id = i;
    }
    sort(a,a + n, cmp);

    for (int j = 0; j < n; j++) {
        if (maxx < a[j].score) {
            maxx = a[j].score;
            w = j;
        }
    }
        cout << a[w].name << " " << a[w].Chinese << " " << a[w].Maths << " " << a[w].English;
    return 0;
}```
是这样的代码但是sort那里会标红不知道哪里错了pnp有大佬能指点一下迷津吗

by hamsterball @ 2021-12-17 10:32:16

cmp函数应该在student后声明


by Bbaka @ 2021-12-17 10:32:45

@cyz_luogu 是不是因为你的结构体定义在了cmp函数下面?


by cyz_luogu @ 2021-12-17 10:35:35

@吴雨航323 谢谢大佬orz


by cyz_luogu @ 2021-12-17 10:36:15

@Bbaka 一句大佬的话=10n分钟的苦思orz


by nndsho @ 2022-08-12 19:15:43

还有似乎sort函数是不稳定排序?


|