Namespaces
Variants

std::ranges:: fold_left_with_iter, std::ranges:: fold_left_with_iter_result

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
fold_left_with_iter
(C++23)
Operations on uninitialized storage
Return types
定義済みヘッダー <algorithm>
呼び出しシグネチャ
(1)
template < std:: input_iterator I, std:: sentinel_for < I > S, class T,

/* indirectly-binary-left-foldable */ < T, I > F >
constexpr /* see description */

fold_left_with_iter ( I first, S last, T init, F f ) ;
(C++23以降)
(C++26まで)
template < std:: input_iterator I, std:: sentinel_for < I > S,

class T = std:: iter_value_t < I > ,
/* indirectly-binary-left-foldable */ < T, I > F >
constexpr /* see description */

fold_left_with_iter ( I first, S last, T init, F f ) ;
(C++26以降)
(2)
template < ranges:: input_range R, class T,

/* 間接的に二項左畳み込み可能 */
< T, ranges:: iterator_t < R >> F >
constexpr /* 説明を参照 */

fold_left_with_iter ( R && r, T init, F f ) ;
(C++23以降)
(C++26まで)
template < ranges:: input_range R, class T = ranges:: range_value_t < R > ,

/* 間接的に二項左畳み込み可能 */
< T, ranges:: iterator_t < R >> F >
constexpr /* 説明を参照 */

fold_left_with_iter ( R && r, T init, F f ) ;
(C++26以降)
ヘルパーコンセプト
template < class F, class T, class I >
concept /* indirectly-binary-left-foldable */ = /* see description */ ;
(3) ( 説明専用* )
ヘルパークラステンプレート
template < class I, class T >
using fold_left_with_iter_result = ranges:: in_value_result < I, T > ;
(4) (C++23以降)

左- 畳み込み は与えられた範囲の要素を左から畳み込み、つまり連鎖式の評価結果を返します:
f(f(f(f(init, x 1 ), x 2 ), ...), x n ) 、ここで x 1 x 2 、...、 x n は範囲の要素です。

非公式には、 ranges::fold_left_with_iter は二項述語を受け入れる std::accumulate のオーバーロードと同様に動作します。

[ first , last ) が有効な範囲でない場合、動作は未定義です。

1) 範囲は [ first , last ) です。
2) (1) と同様であるが、 r を範囲として使用する点が異なる。これは ranges:: begin ( r ) first として、 ranges:: 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) 戻り値型のエイリアス。詳細は「 Return value 」セクションを参照してください。

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる) です。つまり:

目次

パラメータ

first, last - 要素の 範囲 を定義するイテレータ-番兵ペア
r - 畳み込み対象の要素範囲
init - 畳み込みの初期値
f - 二項関数オブジェクト

戻り値

U std:: decay_t < std:: invoke_result_t < F & , T, std:: iter_reference_t < I >>> とする。

1) ranges :: fold_left_with_iter_result < I, U > 型のオブジェクト。
  • メンバ ranges :: in_value_result :: in は範囲の終端を指すイテレータを保持する。
  • メンバ ranges :: in_value_result :: value は与えられた範囲の 左畳み込み の結果を f に対して保持する。
範囲が空の場合、戻り値は以下の式と同等の方法で取得される: return { std :: move ( first ) , U ( std :: move ( init ) ) } ;
2) (1) と同様ですが、戻り値の型は ranges :: fold_left_with_iter_result < ranges:: borrowed_iterator_t < R > , U > です。

実装例

class fold_left_with_iter_fn
{
    template<class O, class I, class S, class T, class F>
    constexpr auto impl(I&& first, S&& last, T&& init, F f) const
    {
        using U = std::decay_t<std::invoke_result_t<F&, T, std::iter_reference_t<I>>>;
        using Ret = ranges::fold_left_with_iter_result<O, U>;
        if (first == last)
            return Ret{std::move(first), U(std::move(init))};
        U accum = std::invoke(f, std::move(init), *first);
        for (++first; first != last; ++first)
            accum = std::invoke(f, std::move(accum), *first);
        return Ret{std::move(first), std::move(accum)};
    }
public:
    template<std::input_iterator I, std::sentinel_for<I> S, class T = std::iter_value_t<I>,
             /* 間接的に二項左畳み込み可能 */<T, I> F>
    constexpr auto operator()(I first, S last, T init, F f) const
    {
        return impl<I>(std::move(first), std::move(last), std::move(init), std::ref(f));
    }
    template<ranges::input_range R, class T = ranges::range_value_t<R>,
             /* 間接的に二項左畳み込み可能 */<T, ranges::iterator_t<R>> F>
    constexpr auto operator()(R&& r, T init, F f) const
    {
        return impl<ranges::borrowed_iterator_t<R>>
        (
            ranges::begin(r), ranges::end(r), std::move(init), std::ref(f)
        );
    }
};
inline constexpr fold_left_with_iter_fn fold_left_with_iter;

計算量

正確に ranges:: distance ( first, last ) 回、関数オブジェクト f を適用します。

注記

以下の表は、すべての制約付き折りたたみアルゴリズムを比較したものです:

畳み込み関数テンプレート 開始方向 初期値 戻り値型
ranges:: fold_left init U
ranges:: fold_left_first 最初の要素 std:: optional < U >
ranges:: fold_right init U
ranges:: fold_right_last 最後の要素 std:: optional < U >
ranges :: fold_left_with_iter 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 最初の要素

(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 <cassert>
#include <complex>
#include <functional>
#include <ranges>
#include <utility>
#include <vector>
int main()
{
    namespace ranges = std::ranges;
    std::vector v{1, 2, 3, 4, 5, 6, 7, 8};
    auto sum = ranges::fold_left_with_iter(v.begin(), v.end(), 6, std::plus<int>());
    assert(sum.value == 42);
    assert(sum.in == v.end());
    auto mul = ranges::fold_left_with_iter(v, 0X69, std::multiplies<int>());
    assert(mul.value == 4233600);
    assert(mul.in == v.end());
    // ベクター内のすべてのペアのstd::pair::secondの積を取得:
    std::vector<std::pair<char, float>> data {{'A', 2.f}, {'B', 3.f}, {'C', 3.5f}};
    auto sec = ranges::fold_left_with_iter
    (
        data | ranges::views::values, 2.0f, std::multiplies<>()
    );
    assert(sec.value == 42);
    // プログラム定義関数オブジェクト(ラムダ式)を使用:
    auto lambda = [](int x, int y){ return x + 0B110 + y; };
    auto val = ranges::fold_left_with_iter(v, -42, lambda);
    assert(val.value == 42);
    assert(val.in == v.end());
    using CD = std::complex<double>;
    std::vector<CD> nums{{1, 1}, {2, 0}, {3, 0}};
    #ifdef __cpp_lib_algorithm_default_value_type
        auto res = ranges::fold_left_with_iter(nums, {7, 0}, std::multiplies{});
    #else
        auto res = ranges::fold_left_with_iter(nums, CD{7, 0}, std::multiplies{});
    #endif
    assert((res.value == CD{42, 42}));
}

参考文献

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

関連項目

要素の範囲を左から畳み込む
(アルゴリズム関数オブジェクト)
最初の要素を初期値として要素の範囲を左から畳み込む
(アルゴリズム関数オブジェクト)
要素の範囲を右から畳み込む
(アルゴリズム関数オブジェクト)
最後の要素を初期値として要素の範囲を右から畳み込む
(アルゴリズム関数オブジェクト)
最初の要素を初期値として要素の範囲を左から畳み込み、 pair (イテレータ, optional )を返す
(アルゴリズム関数オブジェクト)
要素の範囲を合計または畳み込む
(関数テンプレート)
(C++17)
std::accumulate と類似しているが、順序不同
(関数テンプレート)