我有道题让把学生的成绩从大到小排列,如果分数相同,字典序在前。
现在求问怎么在 sort
的 cmp
函数中判断字典序啊,代码如下:
#include<iostream>
#include<algorithm>
using namespace std;
struct student{
string name;
int score;
};
bool cmp(student a,student b){
return a.score>b.score;
if(a.score==b.score) return a.name>b.name;
}
int main(){
student s[100001];
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>s[i].name>>s[i].score;
sort(s+1,s+n+1,cmp);
for(int i=1;i<=n;i++) cout<<s[i].name<<' '<<s[i].score<<endl;
return 0;
}
很菜,不喜勿喷,谢谢。
Ps:之前一直没学 struct