std::ranges::minmax, std::ranges::minmax_result
ja.cppreference.net より
| ヘッダで定義 <algorithm>
|
||
| 呼び出しシグネチャ |
||
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&>
minmax( const T& a, const T& b, Comp comp = {}, Proj proj = {} );
|
(1) | (C++20 以降) |
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>
minmax( std::initializer_list<T> r, Comp comp = {}, Proj proj = {} );
|
(2) | (C++20 以降) |
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>>
minmax( R&& r, Comp comp = {}, Proj proj = {} );
|
(3) | (C++20 以降) |
| ヘルパー型 |
||
template< class T >
using minmax_result = ranges::min_max_result<T>;
|
(4) | (C++20 以降) |
与えられた射影された値の最小値と最大値を返します。
1) の小さい方と大きい方への参照を返します。
a と b.
2) 初期化子リスト内の値の最小値と最大値を返します。
r.
3) 範囲内の値の最小値と最大値を返します。
r.
オーバーロード (2,3) では、範囲が空の場合(ranges::distance(r) によって決定される)、動作は未定義です。
このページで説明される関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式には niebloids として知られる)であり、すなわち:
- それらのいずれかを呼び出すときに明示的なテンプレート引数リストを指定することはできません。
- それらは 引数依存の名前探索(ADL)からは見えません。
- それらのいずれかが 通常の非修飾名探索 によって関数呼び出し演算子の左側の名前として見つかった場合、引数依存の名前探索(ADL) は抑制されます。
パラメータ
| a, b | - | 比較する値 |
| r | - | 比較する値の空でない範囲 |
| comp | - | 投影された要素に適用する比較関数 |
| proj | - | 要素に適用する投影関数 |
戻り値
1)
{
b, a
}
それぞれの射影値に基づいて、
b
が
a
より小さい場合に返される。それ以外の場合は
{
a, b
}
を返す。
2,3)
{
s, l
}
、ここで
s
と
l
はそれぞれ
r
内の最小値と最大値(射影値に基づく)です。複数の値が最小値または最大値と等しい場合、最も左の最小値と最も右の最大値を返します。範囲が空の場合(
ranges::
distance
(
r
)
で判定)、動作は未定義です。
計算量
1)
正確に1回の比較と2回の射影の適用。
2,3)
最大で
3
/
2
*
ranges::
distance
(
r
)
回の比較と、投影関数の2倍の適用回数。
実装例
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つの要素のうち小さい方と大きい方を返す
(関数テンプレート) |