std::ranges:: minmax, std::ranges:: minmax_result
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
<
class
T,
class
Proj
=
std::
identity
,
std::
indirect_strict_weak_order
<
|
(1) | (C++20以降) |
|
template
<
std::
copyable
T,
class
Proj
=
std::
identity
,
std::
indirect_strict_weak_order
<
|
(2) | (C++20以降) |
|
template
<
ranges::
input_range
R,
class
Proj
=
std::
identity
,
std::
indirect_strict_weak_order
<
|
(3) | (C++20以降) |
|
ヘルパー型
|
||
|
template
<
class
T
>
using minmax_result = ranges:: min_max_result < T > ; |
(4) | (C++20以降) |
指定された投影された値の最小値と最大値を返します。
このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:
- 明示的なテンプレート引数リストは、いずれかを呼び出す際に指定できません。
- いずれも 実引数依存の名前探索 では可視になりません。
- いずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって見つかった場合、 実引数依存の名前探索 は抑制されます。
目次 |
パラメータ
| a, b | - | 比較する値 |
| r | - | 比較する値の空でない範囲 |
| comp | - | 投影された要素に適用する比較関数 |
| proj | - | 要素に適用する投影関数 |
戻り値
s
と
l
はそれぞれ
r
内の最小値と最大値(射影値に基づく)です。複数の値が最小値または最大値と等しい場合、最も左の最小値と最も右の最大値を返します。範囲が空の場合(
ranges::
distance
(
r
)
で判定)、動作は未定義です。
計算量
実装例
struct minmax_fn { template<class T, class Proj = std::identity, std::indirect_strict_weak_order< std::projected<const T*, Proj>> Comp = ranges::less> constexpr ranges::minmax_result<const T&> operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const { if (std::invoke(comp, std::invoke(proj, b), std::invoke(proj, a))) return {b, a}; return {a, b}; } template<std::copyable T, class Proj = std::identity, std::indirect_strict_weak_order< std::projected<const T*, Proj>> Comp = ranges::less> constexpr ranges::minmax_result<T> operator()(std::initializer_list<T> r, Comp comp = {}, Proj proj = {}) const { auto result = ranges::minmax_element(r, std::ref(comp), std::ref(proj)); return {*result.min, *result.max}; } template<ranges::input_range R, class Proj = std::identity, std::indirect_strict_weak_order< std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less> requires std::indirectly_copyable_storable<ranges::iterator_t<R>, ranges::range_value_t<R>*> constexpr ranges::minmax_result<ranges::range_value_t<R>> operator()(R&& r, Comp comp = {}, Proj proj = {}) const { auto result = ranges::minmax_element(r, std::ref(comp), std::ref(proj)); return {std::move(*result.min), std::move(*result.max)}; } }; inline constexpr minmax_fn minmax; |
`, `
`, `
注記
オーバーロード
(1)
の場合、いずれかのパラメータが一時オブジェクトである場合、
minmax
の呼び出しを含む完全式の終了時に、返される参照はダングリング参照となります:
int n = 1; auto p = std::ranges::minmax(n, n + 1); int m = p.min; // OK int x = p.max; // 未定義動作 // 構造化束縛も同様の問題を抱えることに注意 auto [mm, xx] = std::ranges::minmax(n, n + 1); xx; // 未定義動作
例
#include <algorithm> #include <array> #include <iostream> #include <random> int main() { namespace ranges = std::ranges; constexpr std::array v{3, 1, 4, 1, 5, 9, 2, 6, 5}; std::random_device rd; std::mt19937_64 generator(rd()); std::uniform_int_distribution<> distribution(0, ranges::distance(v)); // [0..9] // auto bounds = ranges::minmax(distribution(generator), distribution(generator)); // UB: ダングリング参照: bounds.min と bounds.max は `const int&` 型を持つ const int x1 = distribution(generator); const int x2 = distribution(generator); auto bounds = ranges::minmax(x1, x2); // OK: 左辺値 x1 と x2 への参照を取得 std::cout << "v[" << bounds.min << ":" << bounds.max << "]: "; for (int i = bounds.min; i < bounds.max; ++i) std::cout << v[i] << ' '; std::cout << '\n'; auto [min, max] = ranges::minmax(v); std::cout << "smallest: " << min << ", " << "largest: " << max << '\n'; }
出力例:
v[3:9]: 1 5 9 2 6 5 smallest: 1, largest: 9
関連項目
|
(C++20)
|
与えられた値のうち小さい方を返す
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
与えられた値のうち大きい方を返す
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
範囲内の最小要素と最大要素を返す
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
値を境界値のペアの間にクランプする
(アルゴリズム関数オブジェクト) |
|
(C++11)
|
2つの要素のうち小さい方と大きい方を返す
(関数テンプレート) |