std::ranges::minmax_element, std::ranges::minmax_element_result
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_strict_weak_order<std::projected<I, Proj>> Comp = ranges::less > |
(1) | (C++20 起) |
template< ranges::forward_range R, class Proj = std::identity, std::indirect_strict_weak_order< |
(2) | (C++20 起) |
輔助型別 |
||
template< class I > using minmax_element_result = ranges::min_max_result<I>; |
(3) | (C++20 起) |
1) 在範圍
[
first,
last)
中查詢最小和最大元素。本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要檢查的元素 範圍 的迭代器-哨兵對 |
r | - | 要檢查的 range |
comp | - | 應用於投影元素的比較 |
proj | - | 應用於元素的投影。 |
[編輯] 返回值
一個物件,其中第一個元素是指向最小元素的迭代器,第二個元素是指向最大元素的迭代器。如果範圍為空,則返回 {first, first}。如果多個元素等同於最小元素,則返回指向第一個此類元素的迭代器。如果多個元素等同於最大元素,則返回指向最後一個此類元素的迭代器。
[編輯] 複雜度
至多進行 std::max(std::floor(1.5 * (N − 1)), 0.0) 次比較,以及兩倍於此的投影應用,其中 N = ranges::distance(first, last)。
[編輯] 可能的實現
struct minmax_element_fn { template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_strict_weak_order<std::projected<I, Proj>> Comp = ranges::less> constexpr ranges::minmax_element_result<I> operator()(I first, S last, Comp comp = {}, Proj proj = {}) const { auto min = first, max = first; if (first == last || ++first == last) return {min, max}; if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *min))) min = first; else max = first; while (++first != last) { auto i = first; if (++first == last) { if (std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *min))) min = i; else if (!(std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *max)))) max = i; break; } else { if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *i))) { if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *min))) min = first; if (!(std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *max)))) max = i; } else { if (std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *min))) min = i; if (!(std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *max)))) max = first; } } } return {min, max}; } template<ranges::forward_range R, class Proj = std::identity, std::indirect_strict_weak_order< std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less> constexpr ranges::minmax_element_result<ranges::borrowed_iterator_t<R>> operator()(R&& r, Comp comp = {}, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::ref(comp), std::ref(proj)); } }; inline constexpr minmax_element_fn minmax_element; |
[編輯] 示例
執行此程式碼
#include <algorithm> #include <iostream> #include <iterator> namespace ranges = std::ranges; int main() { const auto v = {3, 9, 1, 4, 1, 2, 5, 9}; const auto [min, max] = ranges::minmax_element(v); std::cout << "min = " << *min << ", at [" << ranges::distance(v.begin(), min) << "]\n" << "max = " << *max << ", at [" << ranges::distance(v.begin(), max) << "]\n"; }
輸出
min = 1, at [2] max = 9, at [7]
[編輯] 參閱
(C++20) |
返回一個範圍中最小的元素 (演算法函式物件) |
(C++20) |
返回一個範圍中最大的元素 (演算法函式物件) |
(C++20) |
返回兩個元素中較小和較大的一個 (演算法函式物件) |
(C++11) |
返回範圍中最小和最大的元素 (函式模板) |