std::partial_sort
STD:部分[医]排序
Defined in header | | |
---|---|---|
template< class RandomIt > void partial_sort( RandomIt first, RandomIt middle, RandomIt last | (1) | |
template< class ExecutionPolicy, class RandomIt > void partial_sort( ExecutionPolicy&& policy, RandomIt first, RandomIt middle, RandomIt last | (2) | (since C++17) |
template< class RandomIt, class Compare > void partial_sort( RandomIt first, RandomIt middle, RandomIt last, Compare comp | (3) | |
template< class ExecutionPolicy, class RandomIt, class Compare > void partial_sort( ExecutionPolicy&& policy, RandomIt first, RandomIt middle, RandomIt last, Compare comp | (4) | (since C++17) |
重新排列元素,使范围[first, middle)
包含排序的middle - first
范围内最小的元素[first, last)
...
平等元素的顺序不能得到保证。范围内其余元素的顺序。[middle, 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
复杂性
大约%28最后-第一%29日志%28中-第一%29%29应用程序cmp
...
例外
带有名为ExecutionPolicy
报告错误如下:
- 如果执行作为算法一部分调用的函数,则引发异常
ExecutionPolicy
是其中之一标准政策,,,std::terminate
叫做。对于任何其他人ExecutionPolicy
,行为是由实现定义的。
- 如果算法不能分配内存,
std::bad_alloc
被扔了。
例
二次
#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};
std::partial_sort(s.begin(), s.begin() + 3, s.end()
for (int a : s) {
std::cout << a << " ";
}
}
二次
可能的产出:
二次
0 1 2 7 8 6 5 9 4 3
二次
另见
nth_element | partially sorts the given range making sure that it is partitioned by the given element (function template) |
---|---|
partial_sort_copy | copies and partially sorts a range of elements (function template) |
stable_sort | sorts a range of elements while preserving order between equal elements (function template) |
sort | sorts a range into ascending order (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。