std::ranges::minmax, std::ranges::minmax_result
| ヘッダー <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以降) |
与えられた射影された値の最小値と最大値を返します。
このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[編集] パラメータ
| a, b | - | 比較する値 |
| r | - | 比較対象となる、空でない値のレンジ |
| comp | - | 射影された要素に適用される比較 |
| proj | - | 要素に適用する射影 |
[編集] 戻り値
[編集] 計算量
[編集] 実装例
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; // undefined behavior // Note that structured bindings have the same issue auto [mm, xx] = std::ranges::minmax(n, n + 1); xx; // undefined behavior
[編集] 例
#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: dangling references: bounds.min and bounds.max have the type `const int&`. const int x1 = distribution(generator); const int x2 = distribution(generator); auto bounds = ranges::minmax(x1, x2); // OK: got references to lvalues x1 and 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つの要素の小さい方と大きい方を返す (関数テンプレート) |