结构体简单排序

代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct stu{
    int score,idx;
}s[105];
int cnt;
bool cmp(stu& s1,stu& s2){
    return s1.score!=s2.score?s1.score<s2.score:s1.idx<s2.idx;
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;++i){
            scanf("%d%d",&s[i].idx,&s[i].score);
        }
        cnt=n;
        sort(s+1,s+1+cnt,cmp);
        for(int i=1;i<=n;++i)    printf("%d %d\n",s[i].idx,s[i].score);
    }
    return 0;
}