#include<iostream>
#include <algorithm>
using namespace std;
//方法一:自定义比较子
struct E {
char name[10];
int age;
int score;
};
//按照成绩排序,成绩相同按照名字排序,否则按照年龄排序
bool cmp(E a, E b) {
if (a.score != b.score)
{
return a.score < b.score;
}
else {
int temp = strcmp(a.name, b.name);
if (temp != 0)
return temp < 0;
else
return a.age < b.age;
}
}
int main() {
E student[100] ;
sort(student, student+100, cmp);//按照定义的规则进行排序
E2 student2[100];
sort(student2, student2 + 100);
}
//方法二:通过重载小于等号进行比较
struct E2 {
char name[10];
int age;
int score;
bool operator< (const E2 &b)const {
if (this->score != b.score)
return this->score < b.score;
else {
int temp = strcmp(this->name, b.name);
if (temp != 0)
return temp < 0;
else
return this->age < b.age;
}
}
};