Namespaces
Variants

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

From cppreference.net
< cpp β€Ž | algorithm β€Ž | ranges
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) λ‹€μŒκ³Ό 동등함:
Helper concepts
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 " μ„Ήμ…˜μ„ μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€.

이 νŽ˜μ΄μ§€μ—μ„œ μ„€λͺ…ν•˜λŠ” ν•¨μˆ˜ν˜• κ°œμ²΄λ“€μ€ algorithm function objects (일반적으둜 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 ν΄λ“œ μ•Œκ³ λ¦¬μ¦˜

예제

#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());
    // 벑터 λ‚΄ λͺ¨λ“  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 Fold [alg.fold]

μ°Έκ³  ν•­λͺ©

μš”μ†Œ λ²”μœ„λ₯Ό μ™Όμͺ½μœΌλ‘œ 폴딩
(μ•Œκ³ λ¦¬μ¦˜ ν•¨μˆ˜ 객체)
첫 번째 μš”μ†Œλ₯Ό μ΄ˆκΈ°κ°’μœΌλ‘œ μ‚¬μš©ν•˜μ—¬ μš”μ†Œ λ²”μœ„λ₯Ό μ™Όμͺ½μœΌλ‘œ 폴딩
(μ•Œκ³ λ¦¬μ¦˜ ν•¨μˆ˜ 객체)
μš”μ†Œ λ²”μœ„λ₯Ό 였λ₯Έμͺ½μœΌλ‘œ 폴딩
(μ•Œκ³ λ¦¬μ¦˜ ν•¨μˆ˜ 객체)
λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ΄ˆκΈ°κ°’μœΌλ‘œ μ‚¬μš©ν•˜μ—¬ μš”μ†Œ λ²”μœ„λ₯Ό 였λ₯Έμͺ½μœΌλ‘œ 폴딩
(μ•Œκ³ λ¦¬μ¦˜ ν•¨μˆ˜ 객체)
μš”μ†Œ λ²”μœ„λ₯Ό μ™Όμͺ½μœΌλ‘œ ν΄λ”©ν•˜κ³  pair (반볡자, κ°’)을 λ°˜ν™˜
(μ•Œκ³ λ¦¬μ¦˜ ν•¨μˆ˜ 객체)
μš”μ†Œ λ²”μœ„λ₯Ό ν•©μ‚°ν•˜κ±°λ‚˜ 폴딩
(ν•¨μˆ˜ ν…œν”Œλ¦Ώ)
(C++17)
std::accumulate 와 μœ μ‚¬ν•˜μ§€λ§Œ μˆœμ„œκ°€ 보μž₯λ˜μ§€ μ•ŠμŒ
(ν•¨μˆ˜ ν…œν”Œλ¦Ώ)