- yeyou26 的博客
[-]std sort 多关键字
- 2024-1-18 14:09:40 @
#include<bits/stdc++.h>
using namespace std;
struct student
{
string name;
int score;
}a[105];
bool cmp(student x,student y)
{
if(x.score!=y.score) return x.score<y.score;
else return x.name<y.name;
}
int main()
{
srand(time(0));
for(int i=1;i<=100;i++) a[i].score=rand()%10;
for(int i=1;i<=100;i++)
{
for(int j=1;j<=5;j++) a[i].name+=rand()%26+'a';
}
sort(a+1,a+1+100,cmp);
for(int i=1;i<=100;i++)
{
cout<<a[i].name<<" "<<a[i].score<<endl;
}
return 0;
}