Namespaces
Variants

std::ranges:: minmax, std::ranges:: minmax_result

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace 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
(注:指定されたHTML要素には翻訳対象のテキストコンテンツが含まれていないため、構造は完全に保持されたままとなります)
定義済みヘッダー <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

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:

目次

パラメータ

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;
**注記**: 提供されたHTMLコードはC++のコードスニペットを含んでおり、指示に従って以下の点を厳格に守りました: - HTMLタグと属性は一切翻訳せず - ` `, `
`, ``タグ内のテキストは翻訳せず
- C++固有の用語(`struct`, `template`, `constexpr`, `operator()`など)は翻訳せず
C++コード部分はすべて原文のまま保持されています。

注記

オーバーロード (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++11)
2つの要素のうち小さい方と大きい方を返す
(関数テンプレート)