std:: max
| 
           定义于头文件
            
            
             <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) | 
返回给定值中的较大值。
| 目录 | 
参数
| a, b | - | 要比较的值 | 
| ilist | - | 包含待比较值的初始化列表 | 
| comp | - | 比较函数对象(即满足
         
          
           Compare
          
         
         要求的对象),当
         
          
           a
          
         
         
          小于
         
         
          
           b
          
         
         时返回
         
          
           
            true
           
          
         
         。 比较函数的签名应等价于如下形式: bool cmp ( const Type1 & a, const Type2 & b ) ; 
          虽然签名不需要包含
          
           
            
             const
            
            
             &
            
           
          
          ,但函数不得修改传递给它的对象,且必须能够接受所有(可能为 const 的)
           | 
返回值
复杂度
可能的实现
| 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); } | 
       、
       标签内的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)
             
            
           
           | 将值限制在一对边界值之间 (函数模板) | 
| 
           
            
             
              (C++20)
             
            
           
           | 返回给定值中的较大者 (算法函数对象) |