std::ranges::nth_element
From cppreference.com
| ヘッダー <algorithm> で定義 |
||
| 呼び出しシグネチャ |
||
| template< std::random_access_iterator I, std::sentinel_for<I> S, class Comp = ranges::less, class Proj = std::identity > |
(1) | (C++20以降) |
| template< ranges::random_access_range R, class Comp = ranges::less, class Proj = std::identity > |
(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) <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 と等しいイテレータ。
[編集] 計算量
平均して ranges::distance(first, last) に線形。
[編集] 注釈
通常使用されるアルゴリズムは introselect ですが、適切な平均計算量を持つ他の 選択アルゴリズム も許可されます。
[編集] 可能な実装
msvc stl、libstdc++、および libc++ の実装も参照してください: msvc stl、libstdc++、(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
[編集] 関連項目
| (C++20) |
範囲内で最大の要素を返す (アルゴリズム関数オブジェクト) |
| (C++20) |
範囲内で最小の要素を返す (アルゴリズム関数オブジェクト) |
| (C++20) |
要素の範囲を2つのグループに分割する (アルゴリズム関数オブジェクト) |
| (C++20) |
範囲の最初のN個の要素をソートする (アルゴリズム関数オブジェクト) |
| 与えられた範囲を部分的にソートし、指定された要素によってパーティション化されるようにする (関数テンプレート) |