Namespaces
Variants

std:: max

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
max
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダーで定義 <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
ilist. size ( ) がゼロの場合、または T CopyConstructible でない場合、動作は未定義です。
3) 値の比較に operator < を使用します。
T LessThanComparable でない場合、動作は未定義です。
4) 比較関数 comp を使用して値を比較します。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - その他のテキスト(Parameters、Return value、Complexityなど)はC++関連の専門用語として翻訳せず、原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどはすべて変更せず保持しました - 番号や書式設定も完全に保持しました

パラメータ

a, b - 比較する値
ilist - 比較する値を含む初期化子リスト
comp - 比較関数オブジェクト(すなわち Compare 要件を満たすオブジェクト)。 true を返す場合、 a b より 小さい ことを示す。

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

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

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

戻り値

1,2) a b の大きい方。等価な場合は a を返す。
3,4) ilist 内の最大値。複数の値が最大値と等しい場合、最も左の値を返す。

計算量

1) 正確に1回の比較を operator < を使用して行う。
2) 比較関数の適用は正確に1回 comp .
3,4) 与えられた N ilist. size ( ) として:
3) 厳密に N-1 回の比較を operator < を使用して行う。
4) 厳密に N-1 回の比較関数 comp の適用。

実装例

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 はダングリング参照となる

#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 T CopyConstructible であることが要求されていた
オーバーロード ( 1,2 )
要求されない
LWG 2239 C++98
C++11
1. T LessThanComparable であることが要求されていた
オーバーロード ( 2 ) (C++98) および ( 4 ) (C++11)
2. 計算量の要件が欠落していた
1. 要求されない
2. 要件を追加

関連項目

与えられた値のうち小さい方を返す
(関数テンプレート)
(C++11)
2つの要素のうち小さい方と大きい方を返す
(関数テンプレート)
範囲内の最大要素を返す
(関数テンプレート)
(C++17)
値を境界値のペアの間にクランプする
(関数テンプレート)
与えられた値のうち大きい方を返す
(アルゴリズム関数オブジェクト)