名稱空間
變體
操作

std::min

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
min
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <algorithm>
template< class T >
const T& min( const T& a, const T& b );
(1) (C++14 起為 constexpr)
template< class T, class Compare >
const T& min( const T& a, const T& b, Compare comp );
(2) (C++14 起為 constexpr)
template< class T >
T min( std::initializer_list<T> ilist );
(3) (C++11 起)
(C++14 起為 constexpr)
template< class T, class Compare >
T min( 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 comp(const Type1& a, const Type2& b);

雖然簽名不需要有 const&,但函式不能修改傳遞給它的物件,並且必須能夠接受 Type1Type2 型別(可能為 const)的所有值,無論 值類別 如何(因此,不允許使用 Type1&,除非對於 Type1 移動等同於複製,否則也不允許 Type1(C++11 起))。
型別 Type1Type2 必須能夠讓型別 T 的物件隱式轉換為它們兩者。

[編輯] 返回值

1,2) ab 中較小的一個。若值等價,則返回 a
3,4) ilist 中最小的值。若有多個值與最小值等價,則返回最左側的那個值。

[編輯] 複雜度

1) 恰好一次使用 operator< 的比較。
2) 恰好一次比較函式 comp 的應用。
3,4) 給定 Nilist.size()
3) 恰好使用 operator< 進行 N-1 次比較。
4) 恰好應用比較函式 comp N-1 次。

[編輯] 可能的實現

min (1)
template<class T>
const T& min(const T& a, const T& b)
{
    return (b < a) ? b : a;
}
min (2)
template<class T, class Compare>
const T& min(const T& a, const T& b, Compare comp)
{
    return (comp(b, a)) ? b : a;
}
min (3)
template<class T>
T min(std::initializer_list<T> ilist)
{
    return *std::min_element(ilist.begin(), ilist.end());
}
min (4)
template<class T, class Compare>
T min(std::initializer_list<T> ilist, Compare comp)
{
    return *std::min_element(ilist.begin(), ilist.end(), comp);
}

[編輯] 注意

如果其中一個引數是臨時變數並且返回該引數,則透過引用捕獲 std::min 的結果會產生懸空引用

int n = -1;
const int& r = std::min(n + 2, n * 2); // r is dangling

[編輯] 示例

#include <algorithm>
#include <iostream>
#include <string_view>
 
int main()
{
    std::cout << "smaller of 10 and 010 is " << std::min(10, 010) << '\n'
              << "smaller of 'd' and 'b' is '" << std::min('d', 'b') << "'\n"
              << "shortest of \"foo\", \"bar\", and \"hello\" is \""
              << std::min({"foo", "bar", "hello"},
                          [](const std::string_view s1, const std::string_view s2)
                          {
                              return s1.size() < s2.size();
                          }) << "\"\n";
}

輸出

smaller of 10 and 010 is 8
smaller of 'd' and 'b' is 'b'
shortest of "foo", "bar", and "hello" is "foo"

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 281 C++98 過載 (1,2) 要求 TCopyConstructible 未要求
LWG 2239 C++98
C++11
1. 過載 (2) (C++98) 和 (4) (C++11) 要求 TLessThanComparable
    
2. 缺少複雜度要求
1. 不再要求
2. 增加了要求

[編輯] 參閱

返回給定值中較大的那個
(函式模板) [編輯]
(C++11)
返回兩個元素中較小和較大的一個
(函式模板) [編輯]
返回一個範圍中最小的元素
(函式模板) [編輯]
(C++17)
將值限制在邊界值對之間
(函式模板) [編輯]
返回給定值中較小的那個
(演算法函式物件)[編輯]