std::nth_element
定义于头文件 <algorithm> |
||
template< class RandomIt > void nth_element( RandomIt first, RandomIt nth, RandomIt last ); |
(1) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class RandomIt > void nth_element( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
template< class RandomIt, class Compare > void nth_element( RandomIt first, RandomIt nth, RandomIt last, |
(3) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class RandomIt, class Compare > void nth_element( ExecutionPolicy&& policy, |
(4) | (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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 2150 | C++98 | 重排后,在 nth 之前只有一个元素 要求不大于 nth 之后的一个元素 |
已更正 要求 |
LWG 2163 | C++98 | 重载 (1) 使用 operator> 比较元素 | 改为 operator< |
P0896R4 | C++98 | [ first, nth) 和 [ nth, last) 不再要求为有效范围 |
行为未定义 如果其中任何一个无效 |
[编辑] 参阅
返回一个范围中最大的元素 (函数模板) | |
返回一个范围中最小的元素 (函数模板) | |
复制并部分排序一个范围的元素 (函数模板) | |
对一个范围的元素进行排序,同时保留相等元素之间的顺序 (函数模板) | |
将一个范围按升序排序 (函数模板) | |
(C++20) |
部分排序给定的范围,确保它被给定的元素划分 (算法函数对象) |