std::partial_sort
定义于头文件 <algorithm> |
||
template< class RandomIt > void partial_sort( RandomIt first, RandomIt middle, RandomIt last ); |
(1) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class RandomIt > void partial_sort( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
template< class RandomIt, class Compare > void partial_sort( RandomIt first, RandomIt middle, RandomIt last, |
(3) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class RandomIt, class Compare > void partial_sort( ExecutionPolicy&& policy, |
(4) | (C++17 起) |
重新排列元素,使得范围 [
first,
middle)
包含范围 [
first,
last)
中已排序的 middle − first 个最小元素。
不能保证相等元素的顺序得到保留。范围 [
middle,
last)
中其余元素的顺序未指定。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true。 |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(C++20 起) |
如果满足以下任何条件,则行为是未定义的:
-
[
first,
middle)
或[
middle,
last)
不是有效范围。
|
(C++11 前) |
|
(C++11 起) |
目录 |
[编辑] 参数
first, last | - | 定义要重新排列元素的范围的迭代器对 |
middle | - | 范围 [ first, middle) 将包含已排序的元素 |
policy | - | 要使用的 执行策略 |
comp | - | 比较函数对象(即满足 比较 要求的对象),如果第一个参数“小于”(即在第二个参数“之前”排序),则返回 true。 比较函数的签名应等效于以下内容 bool cmp(const Type1& a, const Type2& b); 虽然签名不需要包含 const&,但函数不得修改传递给它的对象,并且必须能够接受 |
类型要求 | ||
-RandomIt 必须满足 旧版随机访问迭代器 的要求。 | ||
-Compare 必须满足 Compare 的要求。 |
[编辑] 复杂度
给定 M 为 middle - first,N 为 last - first
[编辑] 异常
带有模板参数 ExecutionPolicy
的重载按如下方式报告错误
- 如果作为算法一部分调用的函数执行抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法未能分配内存,则抛出 std::bad_alloc。
[编辑] 可能的实现
partial_sort (1) |
---|
template<typename RandomIt> constexpr //< since C++20 void partial_sort(RandomIt first, RandomIt middle, RandomIt last) { typedef typename std::iterator_traits<RandomIt>::value_type VT; std::partial_sort(first, middle, last, std::less<VT>()); } |
partial_sort (3) |
namespace impl { template<typename RandomIt, typename Compare> constexpr //< since C++20 void sift_down(RandomIt first, RandomIt last, const Compare& comp) { // sift down element at “first” const auto length = static_cast<std::size_t>(last - first); std::size_t current = 0; std::size_t next = 2; while (next < length) { if (comp(*(first + next), *(first + (next - 1)))) --next; if (!comp(*(first + current), *(first + next))) return; std::iter_swap(first + current, first + next); current = next; next = 2 * current + 2; } --next; if (next < length && comp(*(first + current), *(first + next))) std::iter_swap(first + current, first + next); } template<typename RandomIt, typename Compare> constexpr //< since C++20 void heap_select(RandomIt first, RandomIt middle, RandomIt last, const Compare& comp) { std::make_heap(first, middle, comp); for (auto i = middle; i != last; ++i) { if (comp(*i, *first)) { std::iter_swap(first, i); sift_down(first, middle, comp); } } } } // namespace impl template<typename RandomIt, typename Compare> constexpr //< since C++20 void partial_sort(RandomIt first, RandomIt middle, RandomIt last, Compare comp) { impl::heap_select(first, middle, last, comp); std::sort_heap(first, middle, comp); } |
[编辑] 注意
[编辑] 算法
所用的算法通常是“堆选择”以选择最小元素,以及“堆排序”以按升序对堆中选定的元素进行排序。
为了选择元素,使用了一个堆(参见 堆)。例如,对于 operator< 作为比较函数,使用最大堆来选择 middle − first 个最小元素。
选择后使用 堆排序 来对 [
first,
middle)
中选定的元素进行排序(参见 std::sort_heap)。
[编辑] 预期用途
std::partial_sort
算法旨在用于选定的 [
first,
middle)
元素的“少量常数”。
[编辑] 示例
#include <algorithm> #include <array> #include <functional> #include <iostream> void print(const auto& s, int middle) { for (int a : s) std::cout << a << ' '; std::cout << '\n'; if (middle > 0) { while (middle-- > 0) std::cout << "--"; std::cout << '^'; } else if (middle < 0) { for (auto i = s.size() + middle; --i; std::cout << " ") {} for (std::cout << '^'; middle++ < 0; std::cout << "--") {} } std::cout << '\n'; }; int main() { std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; print(s, 0); std::partial_sort(s.begin(), s.begin() + 3, s.end()); print(s, 3); std::partial_sort(s.rbegin(), s.rbegin() + 4, s.rend()); print(s, -4); std::partial_sort(s.rbegin(), s.rbegin() + 5, s.rend(), std::greater{}); print(s, -5); }
可能的输出
5 7 4 2 8 6 1 9 0 3 0 1 2 7 8 6 5 9 4 3 ------^ 4 5 6 7 8 9 3 2 1 0 ^-------- 4 3 2 1 0 5 6 7 8 9 ^----------
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
P0896R4 | C++98 | [ first, middle) 和 [ middle, last) 不需要是有效范围 |
行为未定义 如果其中任何一个无效 |
[编辑] 另请参阅
部分排序给定的范围,确保它被给定的元素划分 (函数模板) | |
复制并部分排序一个范围的元素 (函数模板) | |
对一个范围的元素进行排序,同时保留相等元素之间的顺序 (函数模板) | |
将一个范围按升序排序 (函数模板) | |
(C++20) |
对一个范围的前 N 个元素进行排序 (算法函数对象) |