std::partial_sort_copy
STD:部分[医]排序[医]复制
Defined in header | | |
---|---|---|
template< class InputIt, class RandomIt > RandomIt partial_sort_copy( InputIt first, InputIt last, RandomIt d_first, RandomIt d_last | (1) | |
template< class ExecutionPolicy, class ForwardIt, class RandomIt > RandomIt partial_sort_copy( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, RandomIt d_first, RandomIt d_last | (2) | (since C++17) |
template< class InputIt, class RandomIt, class Compare > RandomIt partial_sort_copy( InputIt first, InputIt last, RandomIt d_first, RandomIt d_last, Compare comp | (3) | |
template< class ExecutionPolicy, class ForwardIt, class RandomIt, class Compare > RandomIt partial_sort_copy( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, RandomIt d_first, RandomIt d_last, Compare comp | (4) | (since C++17) |
对范围内的一些元素进行排序[first, last)
按升序将结果存储在范围内。[d_first, d_last)
...
顶多d_last - d_first
的元素被移动到范围内。[d_first, d_first + n)
然后分类。n
要对%28进行排序的元素数n = min(last - first, d_last - d_first)
29%。平等元素的顺序不能得到保证。
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 |
---|---|---|
d_first, d_last | - | random access iterators defining the destination range |
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. |
类型要求
-输入必须符合输入器的要求。
---。
---。
-取消引用的随机数的类型必须符合可移动分配和移动可建的要求。
返回值
定义排序范围上边界的元素的迭代器,即d_first + min(last - first, d_last - d_first)
...
复杂性
O%28N·log%28 min%28D,N%29%29,其中N =
std::distance
(first, last)
,,,D =
std::distance
(d_first, d_last)
的应用cmp
...
例外
带有名为ExecutionPolicy
报告错误如下:
- 如果执行作为算法一部分调用的函数,则引发异常
ExecutionPolicy
是其中之一标准政策,,,std::terminate
叫做。对于任何其他人ExecutionPolicy
,行为是由实现定义的。
- 如果算法不能分配内存,
std::bad_alloc
被扔了。
例
下面的代码对整数向量进行排序,并将它们复制到一个更小、更大的向量中。
二次
#include <algorithm>
#include <vector>
#include <functional>
#include <iostream>
int main()
{
std::vector<int> v0{4, 2, 5, 1, 3};
std::vector<int> v1{10, 11, 12};
std::vector<int> v2{10, 11, 12, 13, 14, 15, 16};
std::vector<int>::iterator it;
it = std::partial_sort_copy(v0.begin(), v0.end(), v1.begin(), v1.end()
std::cout << "Writing to the smaller vector in ascending order gives: ";
for (int a : v1) {
std::cout << a << " ";
}
std::cout << '\n';
if(it == v1.end())
std::cout << "The return value is the end iterator\n";
it = std::partial_sort_copy(v0.begin(), v0.end(), v2.begin(), v2.end(),
std::greater<int>()
std::cout << "Writing to the larger vector in descending order gives: ";
for (int a : v2) {
std::cout << a << " ";
}
std::cout << '\n' << "The return value is the iterator to " << *it << '\n';
}
二次
产出:
二次
Writing to the smaller vector in ascending order gives: 1 2 3
The return value is the end iterator
Writing to the larger vector in descending order gives: 5 4 3 2 1 15 16
The return value is the iterator to 15
二次
另见
partial_sort | sorts the first N elements of a range (function template) |
---|---|
sort | sorts a range into ascending order (function template) |
stable_sort | sorts a range of elements while preserving order between equal elements (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。