vector sort 使用笔记

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
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include<algorithm>

using namespace std;

bool cmp(int x,int y)
{
return x >y;
}
//sort默认为非降序排序
int main()
{
vector<int>a{2,5,1,4,6};
//正向排序
sort(a.begin(),a.end());
for(auto i:a)
{
cout<<i<<" ";
}
cout<<endl;
//反向排序
sort(a.rbegin(),a.rend());
for(auto i:a)
{
cout<<i<<" ";
}
cout<<endl;
//带cmp参数的排序
sort(a.begin(),a.end(),cmp);
for(auto i:a)
{
cout<<i<<" ";
}
cout<<endl;

// lambda
sort(a.begin(),a.end(),[](int a, int b)-> bool{
return a>b;
// 正序 return a<b;
});
for(auto i:a)
{
cout<<i<<" ";
}
cout<<endl;
sort

参考链接:https://blog.csdn.net/weixin_43547900/article/details/121972917