命名空間
變體
動作

std::max

出自 cppreference.com
< cpp‎ | algorithm
 
 
演算法函式庫
受約束的演算法與範圍演算法 (C++20)
受約束的演算法,例如 ranges::copyranges::sort...
執行策略 (C++17)
排序及相關操作
分割操作
排序操作
二分搜尋操作
(於已分割的範圍上)
集合操作 (針對已排序範圍)
合併操作 (於已排序的範圍上)
堆積操作
最小/最大操作
max
(C++11)
(C++17)
字典序比較操作
排列操作
C 函式庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔 <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) 回傳 ab 之間較大者。
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)Type1Type2 的所有值,無論其值類別為何(因此,不允許使用 Type1&,除非對於 Type1 而言移動等同於複製,否則也不允許使用 Type1(C++11 起))。
型別 Type1Type2 必須滿足型別 T 的物件可隱式轉換為兩者。

[編輯] 回傳值

1,2) ab 之間較大者。若兩者等價,則回傳 a
3,4) ilist 中的最大值。若有多個值與最大值等價,則回傳最左側者。

[編輯] 複雜度

1) 使用 operator< 進行恰好一次比較。
2) 使用比較函式 comp 進行恰好一次應用。
3,4)Nilist.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 的結果會產生懸空參考(dangling reference)。

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. 缺少了複雜度需求
1. 不再要求
2. 加入了相關需求

[編輯] 參閱

回傳給定值中的較小者
(函式模板) [編輯]
(C++11)
回傳兩個元素中的較小值與較大值
(函式模板) [編輯]
回傳範圍內最大的元素
(函式模板) [編輯]
(C++17)
將一個值限制在兩邊界值之間
(函式模板) [編輯]
回傳給定值中的較大者
(演算法函式物件)[編輯]
English Deutsch 日本語 中文(简体) 中文(繁體)