std::nth_element
定义于头文件 <algorithm> |
||
template< class RandomIt > void nth_element( RandomIt first, RandomIt nth, RandomIt last ); |
(1) | (constexpr since C++20) |
template< class ExecutionPolicy, class RandomIt > void nth_element( ExecutionPolicy&& policy, |
(2) | (since C++17) |
template< class RandomIt, class Compare > void nth_element( RandomIt first, RandomIt nth, RandomIt last, |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class RandomIt, class Compare > void nth_element( ExecutionPolicy&& policy, |
(4) | (since C++17) |
nth_element
重新排列范围 [
first,
last)
中的元素,使得重排后:
- 由 nth 指向的元素更改为如果
[
first,
last)
被排序后应该在该位置出现的元素。 - 对于范围
[
first,
nth)
中的每个迭代器 i 和范围[
nth,
last)
中的每个迭代器 j,满足以下条件:
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,
nth)
或[
nth,
last)
不是有效范围。
|
(C++11 前) |
|
(C++11 起) |
内容 |
[编辑] 参数
first, last | - | 定义元素范围以进行部分排序的迭代器对 |
nth | - | 定义排序划分点的随机访问迭代器 |
policy | - | 要使用的执行策略 |
comp | - | 比较函数对象(即,满足Compare要求的对象),如果第一个参数小于(即,排序在之前)第二个参数,则返回 true。 比较函数的签名应等效于以下形式: bool cmp(const Type1& a, const Type2& b); 虽然签名不需要具有 const&,但该函数不得修改传递给它的对象,并且必须能够接受类型为(可能是 const) |
类型要求 | ||
-RandomIt 必须满足 LegacyRandomAccessIterator 的要求。 | ||
-Compare 必须满足 Compare 的要求。 |
[编辑] 复杂度
给定 N 为 last - first
[编辑] 异常
带有名为 ExecutionPolicy
的模板参数的重载按如下方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法无法分配内存,则抛出 std::bad_alloc。
[编辑] 可能的实现
另请参见 libstdc++、libc++ 和 MSVC STL 中的实现。
[编辑] 注意
使用的算法通常是 Introselect,尽管也允许使用其他具有合适平均情况复杂度的选择算法。
[编辑] 示例
#include <algorithm> #include <cassert> #include <functional> #include <iostream> #include <numeric> #include <vector> void printVec(const std::vector<int>& vec) { std::cout << "v = {"; for (char sep[]{0, ' ', 0}; const int i : vec) std::cout << sep << i, sep[0] = ','; std::cout << "};\n"; } int main() { std::vector<int> v{5, 10, 6, 4, 3, 2, 6, 7, 9, 3}; printVec(v); auto m = v.begin() + v.size() / 2; std::nth_element(v.begin(), m, v.end()); std::cout << "\nThe median is " << v[v.size() / 2] << '\n'; // The consequence of the inequality of elements before/after the Nth one: assert(std::accumulate(v.begin(), m, 0) < std::accumulate(m, v.end(), 0)); printVec(v); // Note: comp function changed std::nth_element(v.begin(), v.begin() + 1, v.end(), std::greater{}); std::cout << "\nThe second largest element is " << v[1] << '\n'; std::cout << "The largest element is " << v[0] << '\n'; printVec(v); }
可能的输出
v = {5, 10, 6, 4, 3, 2, 6, 7, 9, 3}; The median is 6 v = {3, 2, 3, 4, 5, 6, 10, 7, 9, 6}; The second largest element is 9 The largest element is 10 v = {10, 9, 6, 7, 6, 3, 5, 4, 3, 2};
[编辑] 缺陷报告
以下行为更改缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2150 | C++98 | 重排后,仅要求 nth 之前的一个元素 不大于 nth 之后的一个元素 |
修正了 要求 |
LWG 2163 | C++98 | 重载 (1) 使用 operator> 比较元素 | 更改为 operator< |
P0896R4 | C++98 | [ first, nth) 和 [ nth, last) 不需要是有效范围 |
如果其中任何一个无效,则行为未定义 如果其中任何一个无效,则行为未定义 |
[编辑] 参见
返回范围中最大的元素 (函数模板) | |
返回范围中最小的元素 (函数模板) | |
复制并部分排序元素范围 (函数模板) | |
对元素范围进行排序,同时保留相等元素之间的顺序 (函数模板) | |
将范围排序为升序 (函数模板) | |
(C++20) |
部分排序给定范围,确保它按给定元素划分 (算法函数对象) |