一、sort()函数中cmp函数的编写
1.在一般的排序中,不需要对cmp函数进行编写,sort()函数在一开始会默认为升序排列。
2.在对结构体类型的数据进行排序时,需要编写cmp()函数。在实际的做题当中,虽然没有cmp函数也可以完成相应的排序,但是利用函数将会更加方便,简洁。
3.cmp函数的返回值类型为bool类型。
#include <iostream>
#include <algorithm>
using namespace std;
const int N=305;
struct student{
int id;
int china;
int math;
int english;
int all;
}stu[N];
bool cmp(student x,student y){
if(x.all!=y.all) return x.all>y.all;
if(x.china!=y.china) return x.china>y.china;
return x.id<y.id;
}//该函数为结构体排序的核心
int main(){
int i,j;
int n;
cin>>n;
for(i=1;i<=n;i++){
cin>>stu[i].china>