彩笔求助struct 用法
  • 板块学术版
  • 楼主Thomas_Cat
  • 当前回复47
  • 已保存回复47
  • 发布时间2020/8/22 20:56
  • 上次更新2023/11/6 19:38:36
查看原帖
彩笔求助struct 用法
326382
Thomas_Cat楼主2020/8/22 20:56

我有道题让把学生的成绩从大到小排列,如果分数相同,字典序在前。

现在求问怎么在 sortcmp 函数中判断字典序啊,代码如下:

#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

2020/8/22 20:56
加载中...