Namespaces
Variants

std:: nth_element

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
(C++11)
nth_element

Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <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 重新排列 [ first , last ) 范围内的元素,使得重排后:

  • nth 指向的元素将被更改为:若 [ first , last ) 区间经过排序,该位置原本会出现的元素。
  • 对于 [ first , nth ) 区间内的每个迭代器 i ,以及 [ nth , last ) 区间内的每个迭代器 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 起)

若满足以下任一条件,则行为未定义:

  • [ 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的) Type1 Type2 类型的值,无论其 值类别 为何(因此不允许 Type1& ,也不允许 Type1 ,除非对于 Type1 类型移动操作等价于拷贝操作 (C++11 起) )。
类型 Type1 Type2 必须使得 RandomIt 类型的对象在解引用后能隐式转换到这两个类型。 ​

类型要求
-
RandomIt 必须满足 LegacyRandomAccessIterator 的要求。
-
Compare 必须满足 Compare 的要求。

复杂度

给定 N last - 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 ,但也允许使用其他具有合适平均情况复杂度的 Selection algorithm

示例

#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';
    // 第N个元素前后元素不等式关系的必然结果:
    assert(std::accumulate(v.begin(), m, 0) < std::accumulate(m, v.end(), 0));
    printVec(v);
    // 注意:比较函数已更改
    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};
中位数为 6
v = {3, 2, 3, 4, 5, 6, 10, 7, 9, 6};
第二大元素为 9
最大元素为 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 )
是有效范围
若任一范围无效
则行为未定义

参见

返回范围内的最大元素
(函数模板)
返回范围内的最小元素
(函数模板)
复制并部分排序范围内的元素
(函数模板)
排序范围内的元素,同时保持相等元素之间的顺序
(函数模板)
将范围按升序排序
(函数模板)
部分排序给定范围,确保其按给定元素进行划分
(算法函数对象)