std::sort的详细用法

https://www.cnblogs.com/hchacha/p/7426824.html

1 #include <algorithm>
 2 #include <functional>
 3 #include <array>
 4 #include <iostream>
 5 
 6 int main()
 7 {
 8     std::array<int, 10> s = { 5, 7, 4, 2, 8, 6, 1, 9, 0, 3 };
 9 
10     // sort using the default operator<
11     std::sort(s.begin(), s.end());
12     for (auto a : s) {
13         std::cout << a << " ";
14     }
15     std::cout << '\n';
16 
17     // sort using a standard library compare function object
18     std::sort(s.begin(), s.end(), std::greater<int>());
19     for (auto a : s) {
20         std::cout << a << " ";
21     }
22     std::cout << '\n';
23 
24     // sort using a custom function object
25     struct {
26         bool operator()(int a, int b) const
27         {
28             return a < b;
29         }
30     } customLess;
31     std::sort(s.begin(), s.end(), customLess);
32     for (auto a : s) {
33         std::cout << a << " ";
34     }
35     std::cout << '\n';
36 
37     // sort using a lambda expression 
38     std::sort(s.begin(), s.end(), [](int a, int b) {
39         return b < a;
40     });
41     for (auto a : s) {
42         std::cout << a << " ";
43     }
44     std::cout << '\n';
45 }
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值