sort使用


一、简单排序

1.基础

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//1.sort顺序排序:默认从小到大排序
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
int num[55] = {0};
num[0] = 9;
num[1] = 2;
num[2] = -5;
num[3] = 5;
num[4] = 7;
num[5] = 22;

//使用sort函数对下标为(0, 5)数字的区间进行排序
sort(num, num + 6);
for(int i = 0; i < 6; ++i){
cout << num[i] << endl;
}
cout << endl;
return 0;
}

2.仿函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//2.sort逆序排序:利用C++中的greater<int>()函数实现
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
int num[55] = {0};
num[0] = 9;
num[1] = 2;
num[2] = -5;
num[3] = 5;
num[4] = 7;
num[5] = 22;

//重点:利用C++中的greater<int>()函数进行从大到小的排序
sort(num, num + 6, greater<int>());
for(int i = 0; i < 6; ++i){
cout << num[i] << endl;
}
cout << edl;
return 0;
}

3.自定义函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//3.sort逆序排序:利用自定义比较函数实现
#include <iostream>
#include <algorithm>
using namespace std;

bool cmp(const int &a, const int &b){
return a > b;//前面的元素值a大于后面的元素值b
}

int main(){
int num[55] = {0};
num[0] = 9;
num[1] = 2;
num[2] = -5;
num[3] = 5;
num[4] = 7;
num[5] = 22;

//重点:利用自定义的比较函数进行从大到小的排序
sort(num, num + 6, cmp);
for(int i = 0; i < 6; ++i){
cout << num[i] << endl;
}
cout << endl;
return 0;
}

4.自定义类型排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
using namespace std;

struct student{
int num, age, score;
string name;
};

bool cmp(const student &a, const student &b){
return a.score > b.score;
}

int main(){
student stu[205];
stu[0].name = "Wu";
stu[0].num = 1;
stu[0].age = 20;
stu[0].score = 60;
stu[1].name = "huang";
stu[1].num = 2;
stu[1].age = 25;
stu[1].score = 150;
stu[2].name = "cui";
stu[2].num = 3;
stu[2].age = 15;
stu[2].score = 50;

//利用cmp函数按照分数score对自定义类型student进行排序
sort(stu, stu + 3, cmp);
for(int i = 0; i < 3; ++i){
cout << stu[i].num << " " << stu[i].name << " " << stu[i].score << endl;
}
return 0;
}

二、复杂类型排序

1.自定义函数

当学生分数相同时,按照学号从大到小排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <algorithm>
using namespace std;

struct student{
int num, age, score;
string name;
};

//如果学生分数相同,则按照学生序号排名
bool cmp(const student &a, const student &b){
if(a.score == b.score){
return a.num < b.num;
}
return a.score > b.score;
}

int main(){
student stu[205];
stu[0].name = "Wu";
stu[0].num = 1;
stu[0].age = 20;
stu[0].score = 60;
stu[1].name = "huang";
stu[1].num = 2;
stu[1].age = 25;
stu[1].score = 150;
stu[2].name = "cui";
stu[2].num = 3;
stu[2].age = 15;
stu[2].score = 60;

//利用cmp函数按照score对自定义类型student进行排序,如果分数相同则按照学号排序
sort(stu, stu + 3, cmp);
for(int i = 0; i < 3; ++i){
cout << stu[i].num << " " << stu[i].name << " " << stu[i].score << endl;
}
return 0;
}

2.重载小于号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <algorithm>
using namespace std;

struct student{
int num, age, score;
string name;
//使用重載小于号
bool op
};

//如果学生分数相同,则按照学生序号排名
bool cmp(const student &a, const student &b){
if(a.score == b.score){
return a.num < b.num;
}
return a.score > b.score;
}

int main(){
student stu[205];
stu[0].name = "Wu";
stu[0].num = 1;
stu[0].age = 20;
stu[0].score = 60;
stu[1].name = "huang";
stu[1].num = 2;
stu[1].age = 25;
stu[1].score = 150;
stu[2].name = "cui";
stu[2].num = 3;
stu[2].age = 15;
stu[2].score = 60;

//利用cmp函数按照score对自定义类型student进行排序,如果分数相同则按照学号排序
sort(stu, stu + 3);
for(int i = 0; i < 3; ++i){
cout << stu[i].num << " " << stu[i].name << " " << stu[i].score << endl;
}
return 0;
}