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 要素より前のすべての要素は、新しい 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 ) の計算量は線形です。

注記

使用されるアルゴリズムは通常 introselect ですが、適切な平均ケース計算量を持つ他の selection algorithms も使用可能です。

実装例

実装については 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

関連項目

範囲内の最大要素を返す
(アルゴリズム関数オブジェクト)
範囲内の最小要素を返す
(アルゴリズム関数オブジェクト)
要素の範囲を2つのグループに分割する
(アルゴリズム関数オブジェクト)
範囲の最初のN個の要素をソートする
(アルゴリズム関数オブジェクト)
指定された要素によって分割されることを保証しながら、指定された範囲を部分的にソートする
(関数テンプレート)