ranges::fold_left_first_with_iter
ranges::fold_left_first_with_iter (반복자와 함께 첫 요소 폴드)
첫 요소 기반 왼쪽 폴드의 결과에 마지막 반복자 정보까지 함께 반환하는 ranges 알고리즘이에요. <algorithm> 헤더, C++23부터.
출처: cppreference
본문
std::ranges::fold_left_first_with_iter는 fold_left_first와 같지만, 결과값과 마지막 반복자를 함께 돌려줘요.
namespace std::ranges {
template< std::input_iterator I, std::sentinel_for<I> S,
std::indirectly_binary_left_foldable<std::iter_value_t<I>, I> F >
constexpr auto fold_left_first_with_iter( I first, S last, F f );
}
반환 타입은 fold_left_first_with_iter_result<I, optional<value_type>> 같은 구조체예요.
.in: 마지막으로 처리한 요소 다음 반복자..value: 최종 누적값. 범위가 비면std::nullopt.
std::vector<int> v{1, 2, 3};
auto res = std::ranges::fold_left_first_with_iter(v, std::plus<>());
// res.in == v.end(), res.value == 6
폴드 결과뿐 아니라 "어디까지 처리했는지"의 반복자까지 필요한 고급 시나리오에 써요.