transform_inclusive_scan

transform_inclusive_scan (변환·포함 누적)

범위의 각 요소에 함수를 적용한 뒤, self-포함(inclusive) 접두 누적을 계산하는 알고리즘이에요. <numeric> 헤더, C++17부터.

출처: cppreference

본문

transform_inclusive_scan[first, last)의 각 요소에 unary_op를 적용한 결과들에 대해 inclusive 접두 누적을 계산해요.

template< class InputIt, class OutputIt, class BinaryOp, class UnaryOp >
OutputIt transform_inclusive_scan( InputIt first, InputIt last,
                                   OutputIt d_first,
                                   BinaryOp binary_op, UnaryOp unary_op );   // (1)
  • 먼저 unary_op로 요소를 변환하고, 그 결과를 binary_op로 누적해요.
  • inclusive라 각 위치는 자기 자신(변환값)까지 포함한 누적을 담아요.
  • binary_op는 결합법칙이 성립해야 해요(병렬 실행 가능). 선택적 init 오버로드도 있어요.
std::vector<int> v{1, 2, 3, 4};
std::vector<int> out(4);
std::transform_inclusive_scan(v.begin(), v.end(), out.begin(),
                              std::plus<>(),
                              [](int x){ return x * x; });
// 제곱값 {1,4,9,16}의 누적: {1,5,14,30}

"변환 + 접두 합계"를 한 번에, 자기 포함으로 구할 때 유용해요.

더 알아보기 (Learn more)

cppreference