std::ranges:: nth_element
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
>
|
(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 の場合、この関数は何も効果を持ちません。
このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:
- 明示的なテンプレート引数リストは、いずれかを呼び出す際に指定できません。
- いずれも 実引数依存の名前探索 では可視になりません。
- いずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって見つかった場合、 実引数依存の名前探索 は抑制されます。
目次 |
パラメータ
| first, last | - | 要素を並べ替える範囲を定義する 範囲 のイテレータ-センチネルペア |
| r | - | 並べ替える要素の範囲 |
| nth | - | 分割点を定義するイテレータ |
| comp | - | 投影された要素を比較するために使用される比較関数 |
| proj | - | 要素に適用する投影関数 |
戻り値
計算量
平均的に 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
関連項目
|
(C++20)
|
範囲内の最大要素を返す
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
範囲内の最小要素を返す
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
要素の範囲を2つのグループに分割する
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
範囲の最初のN個の要素をソートする
(アルゴリズム関数オブジェクト) |
|
指定された要素によって分割されることを保証しながら、指定された範囲を部分的にソートする
(関数テンプレート) |