名稱空間
變體
操作

std::ranges::fold_left_first_with_iter, std::ranges::fold_left_first_with_iter_result

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

          /*indirectly-binary-left-foldable*/<std::iter_value_t<I>, I> F >
requires std::constructible_from<
             std::iter_value_t<I>, std::iter_reference_t<I>>
constexpr /* see description */

    fold_left_first_with_iter( I first, S last, F f );
(1) (C++23 起)
template< ranges::input_range R,

          /*indirectly-binary-left-foldable*/<
              ranges::range_value_t<R>, ranges::iterator_t<R>> F >
requires std::constructible_from<
             ranges::range_value_t<R>, ranges::range_reference_t<R>>
constexpr /* see description */

    fold_left_first_with_iter( R&& r, F f );
(2) (C++23 起)
輔助概念
template< class F, class T, class I >
concept /*indirectly-binary-left-foldable*/ = /* see description */;
(3) (僅作說明*)
Helper class template
template< class I, class T >
using fold_left_first_with_iter_result = ranges::in_value_result<I, T>;
(4) (C++23 起)

對給定範圍的元素進行左摺疊,即返回鏈式表示式求值的結果
f(f(f(f(x1, x2), x3), ...), xn), 其中 x1, x2, ..., xn 是範圍內的元素。

非正式地,ranges::fold_left_first_with_iter 的行為類似於 std::accumulate 的過載,它接受一個二元謂詞,除了 *first 在內部用作初始元素。

如果 [firstlast) 不是有效範圍,則行為未定義。

1) 範圍是 [firstlast)
2)(1),但使用 r 作為範圍,如同使用 ranges::begin(r) 作為 firstranges::end(r) 作為 last
3) 等效於
輔助概念
template< class F, class T, class I, class U >

concept /*indirectly-binary-left-foldable-impl*/ =
    std::movable<T> &&
    std::movable<U> &&
    std::convertible_to<T, U> &&
    std::invocable<F&, U, std::iter_reference_t<I>> &&
    std::assignable_from<U&,

        std::invoke_result_t<F&, U, std::iter_reference_t<I>>>;
(3A) (僅作說明*)
template< class F, class T, class I >

concept /*indirectly-binary-left-foldable*/ =
    std::copy_constructible<F> &&
    std::indirectly_readable<I> &&
    std::invocable<F&, T, std::iter_reference_t<I>> &&
    std::convertible_to<std::invoke_result_t<F&, T, std::iter_reference_t<I>>,
        std::decay_t<std::invoke_result_t<F&, T, std::iter_reference_t<I>>>> &&
    /*indirectly-binary-left-foldable-impl*/<F, T, I,

        std::decay_t<std::invoke_result_t<F&, T, std::iter_reference_t<I>>>>;
(3B) (僅作說明*)
4) 返回類型別名。詳見“返回值”一節。

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

目錄

[編輯] 引數

first, last - 定義要摺疊的元素範圍的迭代器-哨兵對
r - 要摺疊的元素範圍
f - 二元函式物件

[編輯] 返回值

Udecltype(ranges::fold_left(std::move(first), last, std::iter_value_t<I>(*first), f))

1) 型別為 ranges::fold_left_first_with_iter_result<I, std::optional<U>> 的物件。
  • 成員 ranges::in_value_result::in 持有指向範圍末尾的迭代器。
  • 成員 ranges::in_value_result::value 持有給定範圍在 f 上的左摺疊結果。
如果範圍為空,則返回值為 {std::move(first), std::optional<U>()}
2)(1),但返回型別為 ranges::fold_left_first_with_iter_result<ranges::borrowed_iterator_t<R>, std::optional<U>>

[編輯] 可能的實現

class fold_left_first_with_iter_fn
{
    template<class O, class I, class S, class F>
    constexpr auto impl(I&& first, S&& last, F f) const
    {
        using U = decltype(
            ranges::fold_left(std::move(first), last, std::iter_value_t<I>(*first), f)
        );
        using Ret = ranges::fold_left_first_with_iter_result<O, std::optional<U>>;
        if (first == last)
            return Ret{std::move(first), std::optional<U>()};
        std::optional<U> init(std::in_place, *first);
        for (++first; first != last; ++first)
            *init = std::invoke(f, std::move(*init), *first);
        return Ret{std::move(first), std::move(init)};
    }
 
public:
    template<std::input_iterator I, std::sentinel_for<I> S,
             /*indirectly-binary-left-foldable*/<std::iter_value_t<I>, I> F>
    requires std::constructible_from<std::iter_value_t<I>, std::iter_reference_t<I>>
    constexpr auto operator()(I first, S last, F f) const
    {
        return impl<I>(std::move(first), std::move(last), std::ref(f));
    }
 
    template<ranges::input_range R, /*indirectly-binary-left-foldable*/<
        ranges::range_value_t<R>, ranges::iterator_t<R>> F>
    requires
        std::constructible_from<ranges::range_value_t<R>, ranges::range_reference_t<R>>
    constexpr auto operator()(R&& r, F f) const
    {
        return impl<ranges::borrowed_iterator_t<R>>(
            ranges::begin(r), ranges::end(r), std::ref(f)
        );
    }
};
 
inline constexpr fold_left_first_with_iter_fn fold_left_first_with_iter;

[編輯] 複雜度

函式物件 f 的應用次數精確為 ranges::distance(first, last) - 1(假設範圍非空)。

[編輯] 註解

下表比較了所有受限摺疊演算法

摺疊函式模板 開始方向 初始值 返回型別
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>,

其中 BRranges::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>>

其中 BRranges::borrowed_iterator_t<R>

特性測試 標準 特性
__cpp_lib_ranges_fold 202207L (C++23) std::ranges 摺疊演算法

[編輯] 示例

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <ranges>
#include <utility>
#include <vector>
 
int main()
{
    std::vector v{1, 2, 3, 4, 5, 6, 7, 8};
 
    auto sum = std::ranges::fold_left_first_with_iter
    (
        v.begin(), v.end(), std::plus<int>()
    );
    std::cout << "sum: " << sum.value.value() << '\n';
    assert(sum.in == v.end());
 
    auto mul = std::ranges::fold_left_first_with_iter(v, std::multiplies<int>());
    std::cout << "mul: " << mul.value.value() << '\n';
    assert(mul.in == v.end());
 
    // 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', 7.f}};
    auto sec = std::ranges::fold_left_first_with_iter
    (
        data | std::ranges::views::values, std::multiplies<>()
    );
    std::cout << "sec: " << sec.value.value() << '\n';
 
    // use a program defined function object (lambda-expression):
    auto lambda = [](int x, int y) { return x + y + 2; };
    auto val = std::ranges::fold_left_first_with_iter(v, lambda);
    std::cout << "val: " << val.value.value() << '\n';
    assert(val.in == v.end());
}

輸出

sum: 36
mul: 40320
sec: 42
val: 50

[編輯] 參考文獻

  • C++23 標準 (ISO/IEC 14882:2024)
  • 27.6.18 摺疊 [alg.fold]

[編輯] 參閱

對一個範圍的元素進行左摺疊
(演算法函式物件)[編輯]
使用第一個元素作為初始值對一個範圍的元素進行左摺疊
(演算法函式物件)[編輯]
對一個範圍的元素進行右摺疊
(演算法函式物件)[編輯]
使用最後一個元素作為初始值對一個範圍的元素進行右摺疊
(演算法函式物件)[編輯]
對一個範圍的元素進行左摺疊,並返回一個 pair (迭代器, 值)
(演算法函式物件)[編輯]
對一個範圍的元素進行求和或摺疊
(函式模板) [編輯]
(C++17)
類似於 std::accumulate,但順序是亂序的
(函式模板) [編輯]