std::ranges::fold_right
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
(1) | ||
template< std::bidirectional_iterator I, std::sentinel_for<I> S, class T, /* indirectly-binary-right-foldable */<T, I> F > |
(C++23 起) (直到 C++26) |
|
template< std::bidirectional_iterator I, std::sentinel_for<I> S, class T = std::iter_value_t<I>, |
(C++26 起) | |
(2) | ||
template< ranges::bidirectional_range R, class T, /* indirectly-binary-right-foldable */ |
(C++23 起) (直到 C++26) |
|
template< ranges::bidirectional_range R, class T = ranges::range_value_t<R>, /* indirectly-binary-right-foldable */ |
(C++26 起) | |
輔助概念 |
||
template< class F, class T, class I > concept /* indirectly-binary-left-foldable */ = /* see description */; |
(3) | (僅作說明*) |
template< class F, class T, class I > concept /* indirectly-binary-right-foldable */ = /* see description */; |
(4) | (僅作說明*) |
右摺疊給定範圍的元素,即返回鏈式表示式 `f(x₁, f(x₂, ...f(xₙ, init)))` 的求值結果,其中 `x₁`, `x₂`, ..., `xₙ` 是範圍的元素。f(x₁,f(x₂,...f(xₙ,init)))
,其中x₁
,x₂
,...,xₙ
是範圍的元素。
非正式地,`ranges::fold_right` 的行為類似於 ranges::fold_left(views::reverse(r), init, /*flipped*/(f))。
如果 [
first,
last)
不是有效範圍,則行為未定義。
[
first,
last)
。 輔助概念 |
||
template< class F, class T, class I, class U > concept /*indirectly-binary-left-foldable-impl*/ = |
(3A) | (僅作說明*) |
template< class F, class T, class I > concept /*indirectly-binary-left-foldable*/ = |
(3B) | (僅作說明*) |
輔助概念 |
||
template< class F, class T, class I > concept /*indirectly-binary-right-foldable*/ = |
(4A) | (僅作說明*) |
輔助類模板 |
||
template< class F > class /*flipped*/ |
(4B) | (僅作說明*) |
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要摺疊的元素範圍的迭代器-哨兵對 |
r | - | 要摺疊的元素範圍 |
init | - | 摺疊的初始值 |
f | - | 二元函式物件 |
[編輯] 返回值
一個型別為 U 的物件,其中包含給定範圍在 f 上的右摺疊結果,其中 U 等同於 std::decay_t<std::invoke_result_t<F&, std::iter_reference_t<I>, T>>;。
如果範圍為空,則返回 U(std::move(init))。
[編輯] 可能實現
struct fold_right_fn { template<std::bidirectional_iterator I, std::sentinel_for<I> S, class T = std::iter_value_t<I>, /* indirectly-binary-right-foldable */<T, I> F> constexpr auto operator()(I first, S last, T init, F f) const { using U = std::decay_t<std::invoke_result_t<F&, std::iter_reference_t<I>, T>>; if (first == last) return U(std::move(init)); I tail = ranges::next(first, last); U accum = std::invoke(f, *--tail, std::move(init)); while (first != tail) accum = std::invoke(f, *--tail, std::move(accum)); return accum; } template<ranges::bidirectional_range R, class T = ranges::range_value_t<R>, /* indirectly-binary-right-foldable */<T, ranges::iterator_t<R>> F> constexpr auto operator()(R&& r, T init, F f) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(init), std::ref(f)); } }; inline constexpr fold_right_fn fold_right; |
[編輯] 複雜度
函式物件 f 被恰好呼叫 ranges::distance(first, last) 次。
[編輯] 註解
下表比較了所有受限摺疊演算法
摺疊函式模板 | 開始方向 | 初始值 | 返回型別 |
---|---|---|---|
ranges::fold_left | left | init | U |
ranges::fold_left_first | left | 第一個元素 | std::optional<U> |
ranges::fold_right | right | init | U |
ranges::fold_right_last | right | 最後一個元素 | std::optional<U> |
ranges::fold_left_with_iter | left | init |
(1) ranges::in_value_result<I, U> (2) ranges::in_value_result<BR, U>, 其中 BR 為 ranges::borrowed_iterator_t<R> |
ranges::fold_left_first_with_iter | left | 第一個元素 |
(1) ranges::in_value_result<I, std::optional<U>> (2) ranges::in_value_result<BR, std::optional<U>> 其中 BR 為 ranges::borrowed_iterator_t<R> |
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_ranges_fold |
202207L |
(C++23) | std::ranges 摺疊演算法 |
__cpp_lib_algorithm_default_value_type |
202403L |
(C++26) | 演算法的列表初始化 (1,2) |
[編輯] 示例
#include <algorithm> #include <complex> #include <functional> #include <iostream> #include <ranges> #include <string> #include <utility> #include <vector> using namespace std::literals; namespace ranges = std::ranges; int main() { auto v = {1, 2, 3, 4, 5, 6, 7, 8}; std::vector<std::string> vs{"A", "B", "C", "D"}; auto r1 = ranges::fold_right(v.begin(), v.end(), 6, std::plus<>()); // (1) std::cout << "r1: " << r1 << '\n'; auto r2 = ranges::fold_right(vs, "!"s, std::plus<>()); // (2) std::cout << "r2: " << r2 << '\n'; // Use a program defined function object (lambda-expression): std::string r3 = ranges::fold_right ( v, "A", [](int x, std::string s) { return s + ':' + std::to_string(x); } ); std::cout << "r3: " << r3 << '\n'; // Get the product of the std::pair::second of all pairs in the vector: std::vector<std::pair<char, float>> data{{'A', 2.f}, {'B', 3.f}, {'C', 3.5f}}; float r4 = ranges::fold_right ( data | ranges::views::values, 2.0f, std::multiplies<>() ); std::cout << "r4: " << r4 << '\n'; using CD = std::complex<double>; std::vector<CD> nums{{1, 1}, {2, 0}, {3, 0}}; #ifdef __cpp_lib_algorithm_default_value_type auto r5 = ranges::fold_right(nums, {7, 0}, std::multiplies{}); #else auto r5 = ranges::fold_right(nums, CD{7, 0}, std::multiplies{}); #endif std::cout << "r5: " << r5 << '\n'; }
輸出
r1: 42 r2: ABCD! r3: A:8:7:6:5:4:3:2:1 r4: 42 r5: (42,42)
[編輯] 參考
- C++23 標準 (ISO/IEC 14882:2024)
- 27.6.18 摺疊 [alg.fold]
[編輯] 另請參閱
(C++23) |
使用最後一個元素作為初始值對一個範圍的元素進行右摺疊 (演算法函式物件) |
(C++23) |
對一個範圍的元素進行左摺疊 (演算法函式物件) |
(C++23) |
使用第一個元素作為初始值對一個範圍的元素進行左摺疊 (演算法函式物件) |
(C++23) |
對一個範圍的元素進行左摺疊,並返回一個 pair (迭代器, 值) (演算法函式物件) |
左摺疊範圍的元素,使用第一個元素作為初始值,並返回一個對(迭代器,可選) (演算法函式物件) | |
對一個範圍的元素進行求和或摺疊 (函式模板) | |
(C++17) |
類似於 std::accumulate,但順序是亂序的 (函式模板) |