结构体重载比较符号
// 排序规则 按照 1. total 降序 2. cnt 降序 3. id 升序 进行的排序
bool operator<(const Student &t) const {
if (total != t.total) return total > t.total;
if (cnt != t.cnt) return cnt > t.cnt;
return id < t.id;
}
自定义比较函数cmp(调用 sort() 时使用)
// id 升序
bool cmp1(Student a, Student b) {
return a.id < b.id;
}
// 1. name 升序 2. id 升序
bool cmp2(Student a, Student b) {
if(a.name != b.name)
return a.name < b.name;
return a.id < b.id;
}
// 1. grade 升序 2. id 升序
bool cmp3(Student a, Student b) {
if (a.grade != b.grade)
return a.grade < b.grade;
return a.id < b.id;
}