首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::sort

Defined in header <algorithm>

?

?

template< class RandomIt > void sort( RandomIt first, RandomIt last );

(1)

?

template< class ExecutionPolicy, class RandomIt > void sort( ExecutionPolicy&& policy, RandomIt first, RandomIt last );

(2)

(since C++17)

template< class RandomIt, class Compare > void sort( RandomIt first, RandomIt last, Compare comp );

(3)

?

template< class ExecutionPolicy, class RandomIt, class Compare > void sort( ExecutionPolicy&& policy, RandomIt first, RandomIt last, Compare comp );

(4)

(since C++17)

排序范围内的元素[first, last)按升序排列。平等元素的顺序不能得到保证。

1%29个元素的比较operator<...

使用给定的二进制比较函数对3%29个元素进行比较。comp...

2,4%29与%281,3%29相同,但根据policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last

-

the range of elements to sort

policy

-

the execution policy to use. See execution policy for details.

comp

-

comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ?true if the first argument is less than (i.e. is ordered before) the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function object must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type RandomIt can be dereferenced and then implicitly converted to both of them. ?

类型要求

---。

-取消引用的随机数的类型必须符合可移动分配和移动可建的要求。

-比较必须符合比较的要求。

返回值

%280%29

复杂性

O(N·log(N)), where N = std::distance(first, last) comparisons on average.

(until C++11)

O(N·log(N)), where N = std::distance(first, last) comparisons.

(since C++11)

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,会抛出std::bad_alloc

二次

代码语言:javascript
复制
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
 
int main()
{
    std::array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; 
 
    // sort using the default operator<
    std::sort(s.begin(), s.end());
    for (auto a : s) {
        std::cout << a << " ";
    }   
    std::cout << '\n';
 
    // sort using a standard library compare function object
    std::sort(s.begin(), s.end(), std::greater<int>());
    for (auto a : s) {
        std::cout << a << " ";
    }   
    std::cout << '\n';
 
    // sort using a custom function object
    struct {
        bool operator()(int a, int b)
        {   
            return a < b;
        }   
    } customLess;
    std::sort(s.begin(), s.end(), customLess);
    for (auto a : s) {
        std::cout << a << " ";
    }   
    std::cout << '\n';
 
    // sort using a lambda expression 
    std::sort(s.begin(), s.end(), [](int a, int b) {
        return b < a;   
    });
    for (auto a : s) {
        std::cout << a << " ";
    } 
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
0 1 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 1 0

二次

另见

partial_sort

sorts the first N elements of a range (function template)

stable_sort

sorts a range of elements while preserving order between equal elements (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com