Namespaces
Variants

std:: max_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
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダーで定義 <algorithm>
template < class ForwardIt >
ForwardIt max_element ( ForwardIt first, ForwardIt last ) ;
(1) (constexpr since C++17)
template < class ExecutionPolicy, class ForwardIt >

ForwardIt max_element ( ExecutionPolicy && policy,

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

ForwardIt max_element ( ForwardIt first, ForwardIt last,

Compare comp ) ;
(3) (constexpr since C++17)
template < class ExecutionPolicy, class ForwardIt, class Compare >

ForwardIt max_element ( ExecutionPolicy && policy,
ForwardIt first, ForwardIt last,

Compare comp ) ;
(4) (since 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 - 使用する実行ポリシー
comp - 比較関数オブジェクト(つまり 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 の要件を満たさなければなりません。

戻り値

範囲 [ first , last ) 内の最大要素へのイテレータ。範囲内の複数の要素が最大要素と等価である場合、最初のそのような要素へのイテレータを返す。範囲が空の場合は last を返す。

計算量

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

1,2) 厳密に max(N-1,0) 回の比較を operator < (C++20以前) std:: less { } (C++20以降) を使用して行う。
3,4) 厳密に max(N-1,0) 回の比較関数 comp の適用。

例外

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

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

実装例

max_element (1)
template<class ForwardIt>
ForwardIt max_element(ForwardIt first, ForwardIt last)
{
    if (first == last)
        return last;
    ForwardIt largest = first;
    while (++first != last)
        if (*largest < *first)
            largest = first;
    return largest;
}
max_element (3)
template<class ForwardIt, class Compare>
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp)
{
    if (first == last)
        return last;
    ForwardIt largest = first;
    while(++first != last)
        if (comp(*largest, *first))
            largest = first;
    return largest;
}

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
int main()
{
    std::vector<int> v{3, 1, -14, 1, 5, 9, -14, 9};
    std::vector<int>::iterator result;
    result = std::max_element(v.begin(), v.end());
    std::cout << "Max element found at index "
              << std::distance(v.begin(), result)
              << " has value " << *result << '\n';
    result = std::max_element(v.begin(), v.end(), [](int a, int b)
    {
        return std::abs(a) < std::abs(b);
    });
    std::cout << "Absolute max element found at index "
              << std::distance(v.begin(), result)
              << " has value " << *result << '\n';
}

出力:

Max element found at index 5 has value 9
Absolute max element found at index 2 has value -14

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 212 C++98 [ first , last ) が空の場合の戻り値が規定されていなかった この場合 last を返す
LWG 2150 C++98 最初の非最小要素へのイテレータが返されていた 戻り値を修正した

関連項目

範囲内の最小要素を返す
(関数テンプレート)
範囲内の最小要素と最大要素を返す
(関数テンプレート)
指定された値のうち大きい方を返す
(関数テンプレート)
範囲内の最大要素を返す
(アルゴリズム関数オブジェクト)