名稱空間
變體
操作

std::ranges::max

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
max
       
       
排列操作
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <algorithm>
呼叫簽名 (Call signature)
template< class T, class Proj = std::identity,

          std::indirect_strict_weak_order<
              std::projected<const T*, Proj>> Comp = ranges::less >
constexpr const T&

    max( const T& a, const T& b, Comp comp = {}, Proj proj = {} );
(1) (C++20 起)
template< std::copyable T, class Proj = std::identity,

          std::indirect_strict_weak_order<
              std::projected<const T*, Proj>> Comp = ranges::less >
constexpr T

    max( std::initializer_list<T> r, Comp comp = {}, Proj proj = {} );
(2) (C++20 起)
template< ranges::input_range R, class Proj = std::identity,

          std::indirect_strict_weak_order<
              std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less >
requires std::indirectly_copyable_storable<ranges::iterator_t<R>,
                                           ranges::range_value_t<R>*>
constexpr ranges::range_value_t<R>

    max( R&& r, Comp comp = {}, Proj proj = {} );
(3) (C++20 起)

返回給定投影值中較大的那個。

1) 返回 ab 中較大的那個。
2) 返回初始化列表 r 中第一個最大的值。
3) 返回範圍 r 中第一個最大的值。

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

目錄

[編輯] 引數

a, b - 要比較的值
r - 要比較的值的範圍
comp - 應用於投影元素的比較
proj - 應用於元素的投影

[編輯] 返回值

1) 根據各自的投影值,返回 ab 中較大的那個。如果它們等價,返回 a
2,3) 根據投影,返回 r 中最大的值。如果有幾個值與最大值等價,則返回最左邊的一個。如果範圍為空(由 ranges::distance(r) 確定),則行為未定義。

[編輯] 複雜度

1) 恰好一次比較。
2,3) 恰好 ranges::distance(r) - 1 次比較。

[編輯] 可能的實現

struct max_fn
{
    template<class T, class Proj = std::identity,
             std::indirect_strict_weak_order<
                 std::projected<const T*, Proj>> Comp = ranges::less>
    constexpr
    const T& operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const
    {
        return std::invoke(comp, std::invoke(proj, a), std::invoke(proj, b)) ? b : a;
    }
 
    template<std::copyable T, class Proj = std::identity,
             std::indirect_strict_weak_order<
                 std::projected<const T*, Proj>> Comp = ranges::less>
    constexpr
    T operator()(std::initializer_list<T> r, Comp comp = {}, Proj proj = {}) const
    {
        return *ranges::max_element(r, std::ref(comp), std::ref(proj));
    }
 
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_strict_weak_order<
                  std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less>
    requires std::indirectly_copyable_storable<ranges::iterator_t<R>,
                                               ranges::range_value_t<R>*>
    constexpr
    ranges::range_value_t<R> operator()(R&& r, Comp comp = {}, Proj proj = {}) const
    {
        using V = ranges::range_value_t<R>;
        if constexpr (ranges::forward_range<R>)
            return
                static_cast<V>(*ranges::max_element(r, std::ref(comp), std::ref(proj)));
        else
        {
            auto i = ranges::begin(r);
            auto s = ranges::end(r);
            V m(*i);
            while (++i != s)
                if (std::invoke(comp, std::invoke(proj, m), std::invoke(proj, *i)))
                    m = *i;
            return m;
        }
    }
};
 
inline constexpr max_fn max;

[編輯] 注意

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

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

[編輯] 示例

#include <algorithm>
#include <iostream>
#include <string>
 
static_assert(std::ranges::max({0B10, 0X10, 010, 10}) == 16); // overload (2)
 
int main()
{
    namespace ranges = std::ranges;
    using namespace std::string_view_literals;
 
    std::cout << "larger of 1 and 9999: " << ranges::max(1, 9999) << '\n'
              << "larger of 'a', and 'b': '" << ranges::max('a', 'b') << "'\n"
              << "longest of \"foo\", \"bar\", and \"hello\": \""
              << ranges::max({"foo"sv, "bar"sv, "hello"sv}, {},
                             &std::string_view::size) << "\"\n";
}

輸出

larger of 1 and 9999: 9999
larger of 'a', and 'b': 'b'
longest of "foo", "bar", and "hello": "hello"

[編輯] 參閱

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