std::max
From cppreference.com
| ヘッダー <algorithm> で定義 |
||
template< class T > const T& max( const T& a, const T& b ); |
(1) | (C++14以降constexpr) |
template< class T, class Compare > const T& max( const T& a, const T& b, Compare comp ); |
(2) | (C++14以降constexpr) |
template< class T > T max( std::initializer_list<T> ilist ); |
(3) | (C++11以降) (C++14以降constexpr) |
template< class T, class Compare > T max( std::initializer_list<T> ilist, Compare comp ); |
(4) | (C++11以降) (C++14以降constexpr) |
与えられた値のうち大きい方を返します。
1,2) a と b のうち大きい方を返します。
1) operator< を使用して値を比較します。
もし
TがLessThanComparableでない場合、動作は未定義です。2) 比較関数 comp を使用して値を比較します。
3,4) 初期化リスト ilist の値のうち最大のものを返します。
3) operator< を使用して値を比較します。
もし
TがLessThanComparableでない場合、動作は未定義です。4) 比較関数 comp を使用して値を比較します。
目次 |
[編集] パラメータ
| a, b | - | 比較する値 |
| ilist | - | 比較する値を含む初期化子リスト |
| comp | - | 比較関数オブジェクト(つまり、Compare の要件を満たすオブジェクト)で、true を返します。もし a が b より小さい場合。 比較関数のシグネチャは、以下と同等でなければならない。 bool cmp(const Type1& a, const Type2& b); シグネチャにconst&は必須ではないが、関数は渡されたオブジェクトを変更してはならず、値カテゴリに関係なく、 |
[編集] 戻り値
1,2) a と b のうち大きい方。それらが等しい場合は a を返します。
3,4) ilist の中で最大の要素。最大値を持つ要素が複数ある場合、最も左にあるものを返します。
[編集] 計算量
1) operator< を用いた厳密に1回の比較。
2) 比較関数 comp の厳密に1回の適用。
3,4) ilist.size() をNとする。
3) N-1 回の operator< を用いた比較。
4) 比較関数 comp の N-1 回の適用。
[編集] 実装例
| max (1) |
|---|
template<class T> const T& max(const T& a, const T& b) { return (a < b) ? b : a; } |
| max (2) |
template<class T, class Compare> const T& max(const T& a, const T& b, Compare comp) { return (comp(a, b)) ? b : a; } |
| max (3) |
template<class T> T max(std::initializer_list<T> ilist) { return *std::max_element(ilist.begin(), ilist.end()); } |
| max (4) |
template<class T, class Compare> T max(std::initializer_list<T> ilist, Compare comp) { return *std::max_element(ilist.begin(), ilist.end(), comp); } |
[編集] 注意
std::max の結果を参照でキャプチャすると、パラメータのいずれかが一時オブジェクトであり、その一時オブジェクトが返された場合に、ダングリング参照が生じます。
int n = -1; const int& r = std::max(n + 2, n * 2); // r is dangling
[編集] 例
このコードを実行
#include <algorithm> #include <iomanip> #include <iostream> #include <string_view> int main() { auto longest = [](const std::string_view s1, const std::string_view s2) { return s1.size() < s2.size(); }; std::cout << "Larger of 69 and 96 is " << std::max(69, 96) << "\n" "Larger of 'q' and 'p' is '" << std::max('q', 'p') << "'\n" "Largest of 010, 10, 0X10, and 0B10 is " << std::max({010, 10, 0X10, 0B10}) << '\n' << R"(Longest of "long", "short", and "int" is )" << std::quoted(std::max({"long", "short", "int"}, longest)) << '\n'; }
出力
Larger of 69 and 96 is 96 Larger of 'q' and 'p' is 'q' Largest of 010, 10, 0X10, and 0B10 is 16 Longest of "long", "short", and "int" is "short"
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 281 | C++98 | オーバーロード (1,2) では T は CopyConstructible である必要がありました。 | 要求されない |
| LWG 2239 | C++98 C++11 |
1. オーバーロード (2) (C++98) および (4) (C++11) では T は LessThanComparable である必要がありました。 オーバーロード (2) (C++98) および (4) (C++11) 2. 計算量の要件が欠落していました。 |
1. 必要ない 2. 要件が追加されました。 |
[編集] 関連項目
| 与えられた値のうち小さい方を返す (関数テンプレート) | |
| (C++11) |
2つの要素の小さい方と大きい方を返す (関数テンプレート) |
| 範囲内で最大の要素を返す (関数テンプレート) | |
| (C++17) |
値を一対の境界値の間に制限する (関数テンプレート) |
| (C++20) |
与えられた値のうち大きい方を返す (アルゴリズム関数オブジェクト) |