std:: max_element
|
ヘッダーで定義
<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,
|
(2) | (since C++17) |
|
template
<
class
ForwardIt,
class
Compare
>
ForwardIt max_element
(
ForwardIt first, ForwardIt last,
|
(3) | (constexpr since C++17) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
Compare
>
ForwardIt max_element
(
ExecutionPolicy
&&
policy,
|
(4) | (since C++17) |
範囲
[
first
,
last
)
内の最大要素を検索します。
|
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)
|
| 型要件 | ||
-
ForwardIt
は
LegacyForwardIterator
の要件を満たさなければなりません。
|
||
戻り値
範囲
[
first
,
last
)
内の最大要素へのイテレータ。範囲内の複数の要素が最大要素と等価である場合、最初のそのような要素へのイテレータを返す。範囲が空の場合は
last
を返す。
計算量
与えられた N を std:: distance ( first, last ) として:
例外
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 | 最初の非最小要素へのイテレータが返されていた | 戻り値を修正した |
関連項目
|
範囲内の最小要素を返す
(関数テンプレート) |
|
|
(C++11)
|
範囲内の最小要素と最大要素を返す
(関数テンプレート) |
|
指定された値のうち大きい方を返す
(関数テンプレート) |
|
|
(C++20)
|
範囲内の最大要素を返す
(アルゴリズム関数オブジェクト) |