重载小于号的两种方法
方法1:
struct Sum
{
int s, c, d; // s表示c和d的平方
bool operator<(const Sum& t) const //返回较小的那个
{
if(s != t.s) return s < t.s;
if(c != t.c) return c < t.c;
return d < t.d;
}
} sum[N];
sort(sum, sum + n);
方法2:
struct Sum{
int s, c, d;
bool operator()(const Sum & s1, const Sum & s2){
if(s1.s != s2.s) return s1.s < s2.s;
if(s1.c != s2.c) return s1.c < s2.c;
if(s1.d != s2.d) return s1.d < s2.d;
}
}sum[N];
sort(sum, sum + n, Sum());
案例1
#include <iostream>
#include <algorithm>
using namespace std;
//从大到小排序
struct Rule1{
bool operator()(const int & a1, const int & a2){
return a1 > a2;
}
};
//按个位数从小到大排序
struct Rule2{
bool operator()(const int & a1, const int & a2){
return a1 % 10 < a2 % 10;
}
};
void Print(int *a, int size){
for(int i = 0; i < size; i ++){
cout << a[i] << ' ';
}
cout << endl;
}
int main(){
int a[] = {12, 45, 3, 98, 21, 7};
for(auto e : a) cout << e << ' ';
cout << endl;
//从小到大排序
sort(a, a + sizeof a / sizeof (int));
cout << "从小到大排序" << endl;
cout << "1) "; Print(a, sizeof(a) / sizeof(int));
//从大到小排序
sort(a, a + sizeof a / sizeof (int),greater<int>());
cout << "从大到小排序" << endl;
cout << "2) "; Print(a, sizeof(a) / sizeof(int));
//从大到小排序
sort(a, a + sizeof a / sizeof (int), Rule1());
cout << "从大到小排序" << endl;
cout << "3) "; Print(a, sizeof(a) / sizeof(int));
//按余数从小到大排序
sort(a, a + sizeof a / sizeof (int), Rule2());
cout << "按个位从小到大排序" << endl;
cout << "4) "; Print(a, sizeof(a) / sizeof(int));
return 0;
}
结果输出如下:
12 45 3 98 21 7
从小到大排序
1) 3 7 12 21 45 98
从大到小排序
2) 98 45 21 12 7 3
从大到小排序
3) 98 45 21 12 7 3
按个位从小到大排序
4) 21 12 3 45 7 98
案例2
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//学生信息
struct Student{
char name[20];
int id;
double gpa;
};
//学生信息初始化
Student students [] = {
{"Jack", 112, 3.4}, {"Mary", 102, 3.8}, {"Mary", 117, 3.9},
{"Ala", 333, 3.5}, {"Zero", 101, 4.0}
};
//按姓名从小到大排序
struct StudentRule1{
bool operator()(const Student & s1, const Student & s2){
if(strcmp(s1.name, s2.name) < 0){
return true;
}
return false;
}
};
//按id从小到大排序
struct StudentRule2{
bool operator()(const Student & s1, const Student & s2){
return s1.id < s2.id;
}
};
//按gpa从高到低排序
struct StudentRule3{
bool operator()(const Student & s1, const Student & s2){
return s1.gpa > s2.gpa;
}
};
void PrintStudents(Student *s, int size){
for(int i = 0; i < size; i ++){
cout << "(" << s[i].name << "," << s[i].id << " " << s[i].gpa << ")" << ' ';
}
cout << endl;
}
int main(){
cout << "学生信息如下:" << endl;
for(auto e : students){
cout << e.name << ' ' << e.id << ' ' << e.gpa << ' ';
}
cout << endl;
int n = sizeof(students) / sizeof(Student);
cout << "按姓名从小到大排序:" << endl;
sort(students, students + n, StudentRule1());
PrintStudents(students, n);
cout << "按id从小到大排序:" << endl;
sort(students, students + n, StudentRule2());
PrintStudents(students, n);
cout << "按gpa从高到低排序:" << endl;
sort(students, students + n, StudentRule3());
PrintStudents(students, n);
return 0;
}
输出如下:
学生信息如下:
Jack 112 3.4 Mary 102 3.8 Mary 117 3.9 Ala 333 3.5 Zero 101 4
按姓名从小到大排序:
(Ala,333 3.5) (Jack,112 3.4) (Mary,102 3.8) (Mary,117 3.9) (Zero,101 4)
按id从小到大排序:
(Zero,101 4) (Mary,102 3.8) (Jack,112 3.4) (Mary,117 3.9) (Ala,333 3.5)
按gpa从高到低排序:
(Zero,101 4) (Mary,117 3.9) (Mary,102 3.8) (Ala,333 3.5) (Jack,112 3.4)
sto