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 来比较数值。

目录

参数

a, b - 要比较的值
ilist - 包含待比较值的初始化列表
comp - 比较函数对象(即满足 Compare 要求的对象),当 a 小于 b 时返回 true

比较函数的签名应等价于如下形式:

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

虽然签名不需要包含 const & ,但函数不得修改传递给它的对象,且必须能够接受所有(可能为 const 的) Type1 Type2 类型的值,无论其 值类别 为何(因此不允许 Type1 & ,也不允许 Type1 ,除非对于 Type1 移动操作与复制操作等效 (C++11 起) )。
类型 Type1 Type2 必须使得 T 类型的对象能隐式转换到这两个类型。

返回值

1,2) 返回 a b 中的较大值。若二者等价,则返回 a
3,4) 返回 ilist 中的最大值。若存在多个等价的最大值,则返回最左侧的值。

复杂度

1) 仅使用一次 operator < 进行比较。
2) 仅应用比较函数 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);
}
说明:根据要求,所有HTML标签、属性以及
标签内的C++代码均保持原样,仅对表格标题中的英文文本进行了简体中文翻译。翻译时保留了C++专业术语(如template、const等)的原始形式,确保技术文档的准确性和专业性。

注释

当其中一个参数是临时对象且该参数被返回时,通过引用捕获 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 << "69和96中较大的是 " << std::max(69, 96) << "\n"
                 "'q'和'p'中较大的是 '" << std::max('q', 'p') << "'\n"
                 "010、10、0X10和0B10中最大的是 "
              << std::max({010, 10, 0X10, 0B10}) << '\n'
              << R"("long"、"short"和"int"中最长的是 )"
              << std::quoted(std::max({"long", "short", "int"}, longest)) << '\n';
}

输出:

69和96中较大的是 96
'q'和'p'中较大的是 'q'
010、10、0X10和0B10中最大的是 16
"long"、"short"和"int"中最长的是 "short"

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 应用于 发布时行为 正确行为
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)
返回两个元素中的较小者和较大者
(函数模板)
返回范围内的最大元素
(函数模板)
(C++17)
将值限制在一对边界值之间
(函数模板)
返回给定值中的较大者
(算法函数对象)