Namespaces
Variants

std::ranges:: fold_left_first_with_iter, std::ranges:: fold_left_first_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_first_with_iter
(C++23)
Operations on uninitialized storage
Return types
ヘッダーで定義 <algorithm>
呼び出しシグネチャ
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 /* 説明を参照 */

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 /* 説明を参照 */

fold_left_first_with_iter ( R && r, F f ) ;
(2) (C++23以降)
ヘルパーコンセプト
template < class F, class T, class I >
concept /*indirectly-binary-left-foldable*/ = /* 説明を参照 */ ;
(3) ( 説明専用* )
ヘルパークラステンプレート
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(x 1 , x 2 ), x 3 ), ...), x n ) 、ここで x 1 x 2 、...、 x n は範囲の要素です。

非公式には、 ranges::fold_left_first_with_iter は、二項述語を受け入れる std::accumulate のオーバーロードと同様に動作しますが、内部では * first が初期要素として使用される点が異なります。

[ 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 - 畳み込む要素の範囲
f - 二項関数オブジェクト

戻り値

U decltype ( 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;

計算量

正確に ranges:: distance ( first, last ) - 1 回(範囲が空でないと仮定して)関数オブジェクト 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 foldアルゴリズム

#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());
    // ベクター内のすべてのペアのstd::pair::secondの積を取得:
    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';
    // プログラム定義の関数オブジェクト(ラムダ式)を使用:
    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 と類似しているが、順序不同
(関数テンプレート)