std::ranges::minmax, std::ranges::minmax_result
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< class T, class Proj = std::identity, std::indirect_strict_weak_order< |
(1) | (C++20 起) |
template< std::copyable T, class Proj = std::identity, std::indirect_strict_weak_order< |
(2) | (C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_strict_weak_order< |
(3) | (C++20 起) |
輔助型別 |
||
template< class T > using minmax_result = ranges::min_max_result<T>; |
(4) | (C++20 起) |
返回給定投影值的最小和最大值。
1) 返回 a 和 b 中較小值和較大值的引用。
2) 返回初始化列表 r 中最小和最大的值。
3) 返回範圍 r 中最小和最大的值。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
- 呼叫它們中的任何一個時,不能指定顯式模板引數列表。
- 它們中的任何一個都對 引數依賴查詢 不可見。
- 當其中任何一個透過 normal unqualified lookup 作為函式呼叫運算子左側的名稱找到時,將抑制 argument-dependent lookup。
目錄 |
[編輯] 引數
a, b | - | 要比較的值 |
r | - | 一個非空的值範圍,用於比較 |
comp | - | 應用於投影元素的比較 |
proj | - | 應用於元素的投影 |
[編輯] 返回值
1) 如果根據各自的投影值,b 小於 a,則返回 {b, a};否則返回 {a, b}。
2,3) {s, l},其中
s
和 l
分別是 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++20) |
返回給定值中較小的那個 (演算法函式物件) |
(C++20) |
返回給定值中較大的那個 (演算法函式物件) |
(C++20) |
返回範圍中最小和最大的元素 (演算法函式物件) |
(C++20) |
將值限制在邊界值對之間 (演算法函式物件) |
(C++11) |
返回兩個元素中較小和較大的一個 (函式模板) |