名前空間
変種
操作

std::ranges::nth_element

From cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(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) 内の任意のイテレータ ij に対して、式 std::invoke(comp, std::invoke(proj, *j), std::invoke(proj, *i))false と評価されます。
  • もし nth == last ならば、この関数は何も行いません。
1) 要素は、指定された二項比較関数オブジェクト comp およびプロジェクションオブジェクト proj を使用して比較されます。
2) <span class="t-v">(1)</span> と同じですが、<code>r</code> を範囲として使用します。これは <code>ranges::begin(r)</code> を <code>first</code> として、<code>ranges::end(r)</code> を <code>last</code> として使用するのと同等です。

このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、

目次

[編集] パラメータ

first, last - 並べ替える要素の 範囲 を定義するイテレータ-センチネルペア
r - 並べ替える要素の範囲
nth - 分割点を定義するイテレータ
comp - 投影された要素を比較するために使用されるコンパレータ
proj - 要素に適用する射影

[編集] 戻り値

1) last と等しいイテレータ。
2) r が lvalue または borrowed_range 型の場合は、(1) と同じです。それ以外の場合は std::ranges::dangling を返します。

[編集] 計算量

平均して ranges::distance(first, last) に線形。

[編集] 注釈

通常使用されるアルゴリズムは introselect ですが、適切な平均計算量を持つ他の 選択アルゴリズム も許可されます。

[編集] 可能な実装

msvc stl、libstdc++、および libc++ の実装も参照してください: msvc stllibstdc++(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個の要素をソートする
(アルゴリズム関数オブジェクト)[編集]
与えられた範囲を部分的にソートし、指定された要素によってパーティション化されるようにする
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)