名前空間
変種
操作

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

From cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
制約付きアルゴリズム
このメニューのすべての名前は名前空間 `std::ranges` に属します
シーケンスを変更しない操作
シーケンスを変更する操作
パーティション操作
ソート操作
二分探索操作 (ソート済み範囲)
       
       
集合操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
       
       
minmax
順列操作
畳み込み操作
数値演算
(C++23)            
未初期化ストレージに対する操作
戻り値の型
 
ヘッダー <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) ab のうち、それぞれの射影された値に基づいて小さい方と大きい方の参照を返します。
2) 初期化リスト r の値のうち、最小値と最大値を返します。
3) レンジ r の値のうち、最小値と最大値を返します。

このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、

目次

[編集] パラメータ

a, b - 比較する値
r - 比較対象となる、空でない値のレンジ
comp - 射影された要素に適用される比較
proj - 要素に適用する射影

[編集] 戻り値

1) それぞれの射影された値に基づいて、ba より小さい場合、{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; // 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++11)
2つの要素の小さい方と大きい方を返す
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)