transform_exclusive_scan

transform_exclusive_scan (변환·배타 누적)

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

출처: cppreference

본문

transform_exclusive_scan[first, last)의 각 요소에 unary_op를 적용한 결과들에 대해 exclusive 접두 누적을 계산하고, 출력은 init에서 시작해요.

template< class InputIt, class OutputIt, class T,
          class BinaryOp, class UnaryOp >
OutputIt transform_exclusive_scan( InputIt first, InputIt last,
                                   OutputIt d_first, T init,
                                   BinaryOp binary_op, UnaryOp unary_op );   // (1)
  • 먼저 unary_op로 요소를 변환하고, 그 결과를 binary_op로 누적해요.
  • exclusive라 각 위치는 자기 자신(변환값)을 제외한 이전 누적을 담아요. 첫 출력은 init이에요.
  • binary_op는 결합법칙이 성립해야 해요(병렬 실행 가능).
std::vector<int> v{1, 2, 3, 4};
std::vector<int> out(4);
std::transform_exclusive_scan(v.begin(), v.end(), out.begin(), 0,
                              std::plus<>(),
                              [](int x){ return x * x; });
// 제곱값 {1,4,9,16}의 exclusive 누적: {0,1,5,14}

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

더 알아보기 (Learn more)

cppreference