名稱空間
變體
操作

std::ranges::minmax, std::ranges::minmax_result

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
minmax
排列操作
摺疊操作
數值操作
(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 ranges::minmax_result<const T&>

    minmax( 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 ranges::minmax_result<T>

    minmax( 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::minmax_result<ranges::range_value_t<R>>

    minmax( R&& r, Comp comp = {}, Proj proj = {} );
(3) (C++20 起)
輔助型別
template< class T >
using minmax_result = ranges::min_max_result<T>;
(4) (C++20 起)

返回給定投影值的最小和最大值。

1) 返回 ab 中較小值和較大值的引用。
2) 返回初始化列表 r 中最小和最大的值。
3) 返回範圍 r 中最小和最大的值。

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

目錄

[編輯] 引數

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

[編輯] 返回值

1) 如果根據各自的投影值,b 小於 a,則返回 {b, a};否則返回 {a, b}
2,3) {s, l},其中 sl 分別是 r 中根據其投影值最小和最大的值。如果多個值等同於最小和最大值,則返回最左側的最小值和最右側的最大值。如果範圍為空(由 ranges::distance(r) 確定),則行為未定義。

[編輯] 複雜度

1) 恰好進行一次比較和兩次投影應用。
2,3) 最多進行 3 / 2 * ranges::distance(r) 次比較,以及兩倍的投影應用。

[編輯] 可能實現

struct minmax_fn
{
    template<class T, class Proj = std::identity,
             std::indirect_strict_weak_order<
                 std::projected<const T*, Proj>> Comp = ranges::less>
    constexpr ranges::minmax_result<const T&>
         operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const
    {
        if (std::invoke(comp, std::invoke(proj, b), std::invoke(proj, a)))
            return {b, a};
 
        return {a, b};
    }
 
    template<std::copyable T, class Proj = std::identity,
             std::indirect_strict_weak_order<
                 std::projected<const T*, Proj>> Comp = ranges::less>
    constexpr ranges::minmax_result<T>
        operator()(std::initializer_list<T> r, Comp comp = {}, Proj proj = {}) const
    {
        auto result = ranges::minmax_element(r, std::ref(comp), std::ref(proj));
        return {*result.min, *result.max};
    }
 
    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::minmax_result<ranges::range_value_t<R>>
        operator()(R&& r, Comp comp = {}, Proj proj = {}) const
    {
        auto result = ranges::minmax_element(r, std::ref(comp), std::ref(proj));
        return {std::move(*result.min), std::move(*result.max)};
    }
};
 
inline constexpr minmax_fn minmax;

[編輯] 注意

對於過載 (1),如果其中一個引數是臨時物件,則返回的引用在包含對 minmax 呼叫的完整表示式結束時將成為懸空引用。

int n = 1;
auto p = std::ranges::minmax(n, n + 1);
int m = p.min; // ok
int x = p.max; // undefined behavior
 
// Note that structured bindings have the same issue
auto [mm, xx] = std::ranges::minmax(n, n + 1);
xx; // undefined behavior

[編輯] 示例

#include <algorithm>
#include <array>
#include <iostream>
#include <random>
 
int main()
{
    namespace ranges = std::ranges;
 
    constexpr std::array v{3, 1, 4, 1, 5, 9, 2, 6, 5};
 
    std::random_device rd;
    std::mt19937_64 generator(rd());
    std::uniform_int_distribution<> distribution(0, ranges::distance(v)); // [0..9]
 
    // auto bounds = ranges::minmax(distribution(generator), distribution(generator));
    // UB: dangling references: bounds.min and bounds.max have the type `const int&`.
 
    const int x1 = distribution(generator);
    const int x2 = distribution(generator);
    auto bounds = ranges::minmax(x1, x2); // OK: got references to lvalues x1 and x2
 
    std::cout << "v[" << bounds.min << ":" << bounds.max << "]: ";
    for (int i = bounds.min; i < bounds.max; ++i)
        std::cout << v[i] << ' ';
    std::cout << '\n';
 
    auto [min, max] = ranges::minmax(v);
    std::cout << "smallest: " << min << ", " << "largest: " << max << '\n';
}

可能的輸出

v[3:9]: 1 5 9 2 6 5 
smallest: 1, largest: 9

[編輯] 另請參閱

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