Namespaces
Variants

std::ranges:: 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
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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
template < std:: random_access_iterator I, std:: sentinel_for < I > S,

class Comp = ranges:: less , class Proj = std:: identity >
requires std:: sortable < I, Comp, Proj >
constexpr I

nth_element ( I first, I nth, S last, Comp comp = { } , Proj proj = { } ) ;
(1) (C++20 起)
template < ranges:: random_access_range R,

class Comp = ranges:: less , class Proj = std:: identity >
requires std:: sortable < iterator_t < R > , Comp, Proj >
constexpr ranges:: borrowed_iterator_t < R >

nth_element ( R && r, iterator_t < R > nth, Comp comp = { } , Proj proj = { } ) ;
(2) (C++20 起)

[ first , last ) 范围内的元素进行重新排序,使得:

  • nth 指向的元素将被更改为:若对区间 [ first , last ) 按照 comp proj 进行排序时,该位置原本会出现的元素。
  • 位于新 nth 元素之前的所有元素均小于或等于该元素之后的元素。即对于分别位于区间 [ first , nth ) [ nth , last ) 的任意迭代器 i j ,表达式 std:: invoke ( comp, std:: invoke ( proj, * j ) , std:: invoke ( proj, * i ) ) 的求值结果均为 false
  • nth == last ,则此函数无任何效果。
1) 元素通过给定的二元比较函数对象 comp 和投影对象 proj 进行比较。
2) (1) 相同,但使用 r 作为范围,如同将 ranges:: begin ( r ) 用作 first ,并将 ranges:: end ( r ) 用作 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待重排元素范围的 区间 迭代器-哨位对
r - 待重排的元素范围
nth - 定义分区点的迭代器
comp - 用于比较投影元素的比较器
proj - 应用于元素的投影

返回值

1) 一个等于 last 的迭代器。
2) (1) 相同,若 r 是左值或属于 borrowed_range 类型。否则返回 std::ranges::dangling

复杂度

平均而言,时间复杂度与 ranges:: distance ( first, last ) 呈线性关系。

注释

所采用的算法通常是 内省选择 ,但也允许使用其他具有合适平均情况复杂度的 选择算法

可能的实现

另请参阅 msvc stl libstdc++ 以及 libc++ 中的实现: (1) / (2)

示例

#include <algorithm>
#include <array>
#include <functional>
#include <iostream>
#include <ranges>
#include <string_view>
void print(std::string_view rem, std::ranges::input_range auto const& a)
{
    for (std::cout << rem; const auto e : a)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    std::array v{5, 6, 4, 3, 2, 6, 7, 9, 3};
    print("Before nth_element: ", v);
    std::ranges::nth_element(v, v.begin() + v.size() / 2);
    print("After nth_element:  ", v);
    std::cout << "The median is: " << v[v.size() / 2] << '\n';
    std::ranges::nth_element(v, v.begin() + 1, std::greater<int>());
    print("After nth_element:  ", v);
    std::cout << "The second largest element is: " << v[1] << '\n';
    std::cout << "The largest element is: " << v[0] << "\n\n";
    using namespace std::literals;
    std::array names
    {
        "Diva"sv, "Cornelius"sv, "Munro"sv, "Rhod"sv,
        "Zorg"sv, "Korben"sv, "Bender"sv, "Leeloo"sv,
    };
    print("Before nth_element: ", names);
    auto fifth_element{std::ranges::next(names.begin(), 4)};
    std::ranges::nth_element(names, fifth_element);
    print("After nth_element:  ", names);
    std::cout << "The 5th element is: " << *fifth_element << '\n';
}

输出:

Before nth_element: 5 6 4 3 2 6 7 9 3 
After nth_element:  2 3 3 4 5 6 6 7 9 
The median is: 5
After nth_element:  9 7 6 6 5 4 3 3 2 
The second largest element is: 7
The largest element is: 9
Before nth_element: Diva Cornelius Munro Rhod Zorg Korben Bender Leeloo 
After nth_element:  Diva Cornelius Bender Korben Leeloo Rhod Munro Zorg 
The 5th element is: Leeloo

参见

返回范围中的最大元素
(算法函数对象)
返回范围中的最小元素
(算法函数对象)
将元素范围划分为两组
(算法函数对象)
对范围的前N个元素进行排序
(算法函数对象)
部分排序给定范围,确保其按指定元素划分
(函数模板)