命名空间
变体
操作

std::nth_element

来自 cppreference.cn
< cpp‎ | 算法
 
 
算法库
有约束算法与针对范围的算法 (C++20)
有约束的算法,例如 ranges::copyranges::sort 等……
执行策略 (C++17)
排序及相关操作
划分操作
排序操作
(C++11)
nth_element

二分搜索操作
(于已划分范围上)
集合操作(于已排序范围上)
归并操作(于已排序范围上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
定义于头文件 <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,

                  RandomIt first, RandomIt nth, RandomIt last );
(2) (C++17 起)
template< class RandomIt, class Compare >

void nth_element( RandomIt first, RandomIt nth, RandomIt last,

                  Compare comp );
(3) (C++20 起为 constexpr)
template< class ExecutionPolicy, class RandomIt, class Compare >

void nth_element( ExecutionPolicy&& policy,
                  RandomIt first, RandomIt nth, RandomIt last,

                  Compare comp );
(4) (C++17 起)

nth_element 重新排列 [firstlast) 中的元素,使得重排后

  • nth 指向的元素变为如果 [firstlast) 排序后在该位置上出现的元素。
  • 对于 [firstnth) 中的每个迭代器 i[nthlast) 中的每个迭代器 j,满足以下条件:
1,2) bool(*j < *i)(C++20 前)std::less{}(*j, *i)(C++20 起)false
3,4) bool(comp(*j, *i))false


1) 元素根据 operator<(C++20 前)std::less{}(C++20 起) 假定已排序
3) 元素根据 comp 假定已排序。
2,4)(1,3),但按 policy 执行。
仅当满足所有以下条件时,这些重载才参与重载决议

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 起)

如果满足以下任何条件,则行为是未定义的:

(C++11 前)
(C++11 起)

目录

[编辑] 参数

first, last - 定义用于部分排序的元素范围的迭代器对。
nth - 定义排序分区点的随机访问迭代器。
policy - 要使用的 执行策略
comp - 比较函数对象(即满足比较 (Compare)要求,并返回 true 如果第一个参数“小于”(即排在“之前”)第二个参数的对象)。

比较函数的签名应等效于以下内容

bool cmp(const Type1& a, const Type2& b);

虽然签名不需要有 const&,但该函数不能修改传递给它的对象,并且必须能够接受所有类型为 (可能是 const) Type1Type2 的值,无论其值类别如何(因此,不允许使用 Type1&,也不允许使用 Type1,除非对于 Type1,移动等同于复制(C++11 起))。
类型 Type1Type2 必须是这样的:类型为 RandomIt 的对象可以被解引用,然后隐式转换为这两种类型。​

类型要求
-
RandomIt 必须满足旧式随机访问迭代器 (LegacyRandomAccessIterator)的要求。
-
Compare 必须满足 Compare 的要求。

[编辑] 复杂度

给定 Nlast - first

1) 平均进行 O(N) 次比较,使用 operator<(C++20 前)std::less{}(C++20 起)
2) 进行 O(N) 次比较,使用 operator<(C++20 前)std::less{}(C++20 起),以及 O(N·log(N)) 次交换。
3) 平均进行 O(N) 次比较器 comp 的应用。
4) 进行 O(N) 次比较器 comp 的应用,以及 O(N·log(N)) 次交换。

[编辑] 异常

带有模板参数 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 [firstnth)[nthlast)
不再要求为有效范围
行为未定义
如果其中任何一个无效

[编辑] 参阅

返回一个范围中最大的元素
(函数模板) [编辑]
返回一个范围中最小的元素
(函数模板) [编辑]
复制并部分排序一个范围的元素
(函数模板) [编辑]
对一个范围的元素进行排序,同时保留相等元素之间的顺序
(函数模板) [编辑]
将一个范围按升序排序
(函数模板) [编辑]
部分排序给定的范围,确保它被给定的元素划分
(算法函数对象)[编辑]