std::max_element
| ヘッダー <algorithm> で定義 |
||
template< class ForwardIt > ForwardIt max_element( ForwardIt first, ForwardIt last ); |
(1) | (C++17 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt > ForwardIt max_element( ExecutionPolicy&& policy, |
(2) | (C++17以降) |
template< class ForwardIt, class Compare > ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp ); |
(3) | (C++17 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class Compare > ForwardIt max_element( ExecutionPolicy&& policy, |
(4) | (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以降) |
目次 |
[edit] パラメータ
| first, last | - | 検査する要素の範囲を定義するイテレータのペア |
| policy | - | 使用する 実行ポリシー |
| comp | - | 比較関数オブジェクト(すなわち、Compareの要件を満たすオブジェクト)。最初の引数が2番目の引数より小さい場合にtrueを返す。 比較関数のシグネチャは、以下と同等でなければならない。 bool cmp(const Type1& a, const Type2& b); シグネチャにconst&は必須ではないが、関数は渡されたオブジェクトを変更してはならず、値カテゴリに関係なく、 |
| 型要件 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 | ||
[edit] 戻り値
範囲 [first, last) 内の最大の要素へのイテレータ。範囲内に最大の要素と同等な要素が複数ある場合、そのような要素の最初のものへのイテレータが返されます。範囲が空の場合、last が返されます。
[edit] 計算量
std::distance(first, last) を N とする
[edit] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[edit] 実装例
| 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; } |
[edit] 例
#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
[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 212 | C++98 | 範囲 [first, last) が空の場合の戻り値が指定されていませんでした。 |
この場合、last が返されます。 |
| LWG 2150 | C++98 | 最小でない要素の最初のものへのイテレータが返されていました。 | 戻り値が修正されました。 |
[edit] 関連項目
| 範囲内で最小の要素を返す (関数テンプレート) | |
| (C++11) |
範囲内で最小の要素と最大の要素を返す (関数テンプレート) |
| 与えられた値のうち大きい方を返す (関数テンプレート) | |
| (C++20) |
範囲内で最大の要素を返す (アルゴリズム関数オブジェクト) |