Namespaces
Variants

std:: minmax_element

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
minmax_element
(C++11)

Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダー <algorithm> で定義
template < class ForwardIt >

std:: pair < ForwardIt, ForwardIt >

minmax_element ( ForwardIt first, ForwardIt last ) ;
(1) (C++11以降)
(constexpr C++17以降)
template < class ExecutionPolicy, class ForwardIt >

std:: pair < ForwardIt, ForwardIt >
minmax_element ( ExecutionPolicy && policy,

ForwardIt first, ForwardIt last ) ;
(2) (C++17以降)
template < class ForwardIt, class Compare >

std:: pair < ForwardIt, ForwardIt >

minmax_element ( ForwardIt first, ForwardIt last, Compare comp ) ;
(3) (C++11以降)
(constexpr C++17以降)
template < class ExecutionPolicy, class ForwardIt, class Compare >

std:: pair < ForwardIt, ForwardIt >
minmax_element ( ExecutionPolicy && policy,

ForwardIt first, ForwardIt last, Compare comp ) ;
(4) (C++17以降)

範囲 [ first , last ) 内の最小要素と最大要素を検索します。

1) 要素は operator < (until C++20) std:: less { } (since C++20) を使用して比較されます。
3) 要素は比較関数 comp を使用して比較されます。
2,4) (1,3) と同様ですが、 policy に従って実行されます。
これらのオーバーロードは、以下のすべての条件が満たされる場合にのみオーバーロード解決に参加します:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true であること。

(C++20まで)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true であること。

(C++20以降)

目次

パラメータ

first, last - 検査する要素の範囲を定義するイテレータのペア
policy - 使用する実行ポリシー
cmp - 比較関数オブジェクト(つまり Compare 要件を満たすオブジェクト)。最初の引数が2番目の引数より 小さい 場合に true を返す。

比較関数のシグネチャは以下と同等であるべき:

bool cmp ( const Type1 & a, const Type2 & b ) ;

シグネチャが const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、 値カテゴリ に関係なく型(おそらくconst) Type1 Type2 のすべての値を受け入れられなければならない(したがって、 Type1 & は許可されない 、また Type1 も、 Type1 に対してムーブがコピーと等価でない限り許可されない (C++11以降) )。
Type1 Type2 は、 ForwardIt 型のオブジェクトが間接参照され、それら両方に暗黙的に変換可能でなければならない。

型要件
-
ForwardIt LegacyForwardIterator の要件を満たさなければならない。

戻り値

最小要素へのイテレータを第一要素、最大要素へのイテレータを第二要素とするペアを返す。範囲が空の場合、 std:: make_pair ( first, first ) を返す。最小要素に等しい要素が複数ある場合、最初のそのような要素へのイテレータが返される。最大要素に等しい要素が複数ある場合、最後のそのような要素へのイテレータが返される。

計算量

与えられた N std:: distance ( first, last ) として:

1,2) 最大 max(⌊
3
2
(N-1)⌋,0)
回の比較を operator < (C++20まで) std:: less { } (C++20以降) を使用して行う。
3,4) 最大で max(⌊
3
2
(N-1)⌋,0)
回の比較関数 comp の適用。

例外

ExecutionPolicy という名前のテンプレートパラメータを持つオーバーロードは、 以下のようにエラーを報告します:

  • アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、 ExecutionPolicy 標準ポリシー のいずれかであるとき、 std::terminate が呼び出される。それ以外の ExecutionPolicy については、動作は実装定義である。
  • アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされる。

実装例

minmax_element
template<class ForwardIt>
std::pair<ForwardIt, ForwardIt>
    minmax_element(ForwardIt first, ForwardIt last)
{
    using value_type = typename std::iterator_traits<ForwardIt>::value_type;
    return std::minmax_element(first, last, std::less<value_type>());
}
minmax_element
template<class ForwardIt, class Compare>
std::pair<ForwardIt, ForwardIt>
    minmax_element(ForwardIt first, ForwardIt last, Compare comp)
{
    auto min = first, max = first;
    if (first == last || ++first == last)
        return {min, max};
    if (comp(*first, *min))
        min = first;
    else
        max = first;
    while (++first != last)
    {
        auto i = first;
        if (++first == last)
        {
            if (comp(*i, *min))
                min = i;
            else if (!(comp(*i, *max)))
                max = i;
            break;
        }
        else
        {
            if (comp(*first, *i))
            {
                if (comp(*first, *min))
                    min = first;
                if (!(comp(*i, *max)))
                    max = i;
            }
            else
            {
                if (comp(*i, *min))
                    min = i;
                if (!(comp(*first, *max)))
                    max = first;
            }
        }
    }
    return {min, max};
}

注記

このアルゴリズムは、 std:: make_pair ( std:: min_element ( ) , std:: max_element ( ) ) とは効率だけでなく、このアルゴリズムが 最後の 最大要素を見つけるのに対し、 std::max_element 最初の 最大要素を見つける点でも異なります。

#include <algorithm>
#include <iostream>
int main()
{
    const auto v = {3, 9, 1, 4, 2, 5, 9};
    const auto [min, max] = std::minmax_element(begin(v), end(v));
    std::cout << "min = " << *min << ", max = " << *max << '\n';
}

出力:

min = 1, max = 9

関連項目

範囲内の最小要素を返す
(関数テンプレート)
範囲内の最大要素を返す
(関数テンプレート)
範囲内の最小要素と最大要素を返す
(アルゴリズム関数オブジェクト)