std::partial_sort
定义于头文件 <algorithm> |
||
template< class RandomIt > void partial_sort( RandomIt first, RandomIt middle, RandomIt last ); |
(1) | (constexpr since C++20) |
template< class ExecutionPolicy, class RandomIt > void partial_sort( ExecutionPolicy&& policy, |
(2) | (since C++17) |
template< class RandomIt, class Compare > void partial_sort( RandomIt first, RandomIt middle, RandomIt last, |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class RandomIt, class Compare > void partial_sort( ExecutionPolicy&& policy, |
(4) | (since 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 | - | 比较函数对象 (即,满足 比较 (Compare) 要求的对象),如果第一个参数小于(即,排序在之前)第二个参数,则返回 true。 比较函数的签名应等效于以下内容 bool cmp(const Type1& a, const Type2& b); 虽然签名不需要具有 const&,但该函数不得修改传递给它的对象,并且必须能够接受类型为 |
类型要求 | ||
-RandomIt 必须满足 LegacyRandomAccessIterator 的要求。 | ||
-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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
P0896R4 | C++98 | [ first, middle) 和 [ middle, last) 未要求为有效范围 |
如果其中任何一个无效,则行为未定义 如果其中任何一个无效 |
[编辑] 参见
部分排序给定范围,确保它按给定元素划分 (函数模板) | |
复制并部分排序元素范围 (函数模板) | |
对元素范围进行排序,同时保留相等元素之间的顺序 (函数模板) | |
将范围排序为升序 (函数模板) | |
(C++20) |
对范围的前 N 个元素进行排序 (算法函数对象) |