命名空间
变体
操作

std::ranges::nth_element

来自 cppreference.cn
< cpp‎ | algorithm‎ | ranges
 
 
算法库
约束算法和范围上的算法 (C++20)
约束算法,例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作
(在已划分范围上)
集合操作 (在已排序范围上)
归并操作 (在已排序范围上)
堆操作
最小值/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
约束算法
此菜单中的所有名称均属于命名空间 std::ranges
非修改序列操作
修改序列操作
划分操作
排序操作
       
     
nth_element
二分搜索操作 (在已排序范围上)
       
       
集合操作 (在已排序范围上)
堆操作
最小值/最大值操作
       
       
排列操作
折叠操作
数值操作
(C++23)            
未初始化存储上的操作
返回类型
 
在头文件 <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 起)

重排范围 [firstlast) 中的元素,使得

  • nth 指向的元素更改为如果范围 [firstlast) 按照 compproj 排序后,该位置将出现的元素。
  • 所有在这个新的 nth 元素之前的元素都小于或等于新的 nth 元素之后的元素。 也就是说,对于范围 [firstnth)[nthlast) 中的每个迭代器 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) 作为 firstranges::end(r) 作为 last

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

内容

[编辑] 参数

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

[编辑] 返回值

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

[编辑] 复杂度

平均情况下与 ranges::distance(first, last) 成线性关系。

[编辑] 注解

所用算法通常是 introselect,尽管也允许使用其他具有适当平均情况复杂度的选择算法

[编辑] 可能的实现

另请参见 msvc stllibstdc++ 和 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 个元素进行排序
(算法函数对象)[编辑]
部分排序给定范围,确保它按给定元素划分
(函数模板) [编辑]