Namespaces
Variants

std:: transform_inclusive_scan

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
transform_inclusive_scan
(C++17)

Operations on uninitialized memory
헤더 파일에 정의됨 <numeric>
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) (C++17부터)
(C++20부터 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2,
class BinaryOp, class UnaryOp >
ForwardIt2 transform_inclusive_scan
( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,

BinaryOp binary_op, UnaryOp unary_op ) ;
(2) (C++17부터)
template < class InputIt, class OutputIt,

class BinaryOp, class UnaryOp, class T >
OutputIt transform_inclusive_scan
( InputIt first, InputIt last, OutputIt d_first,

BinaryOp binary_op, UnaryOp unary_op, T init ) ;
(3) (C++17부터)
(C++20부터 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2,
class BinaryOp, class UnaryOp, class T >
ForwardIt2 transform_inclusive_scan
( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,

BinaryOp binary_op, UnaryOp unary_op, T init ) ;
(4) (C++17부터)
1) op 을 사용하여 포함적 접두사 합을 계산합니다.
각 정수 i 에 대해 [ 0 , std:: distance ( first, last ) ) 범위 내에서, 다음 연산들을 순서대로 수행합니다:
  1. [ first , iter ] 범위의 요소들을 unary_op 로 변환한 값들로 구성된 시퀀스를 생성합니다. 여기서 iter first 의 다음 i 번째 반복자입니다.
  2. 생성된 시퀀스에 대해 binary_op 를 사용한 일반화된 비가환 합을 계산합니다.
  3. 결과를 * dest 에 할당합니다. 여기서 dest d_first 의 다음 i 번째 반복자입니다.
3) (1) 과 동일하지만, 생성된 각 시퀀스는 init 뒤에 [ first , iter ] 범위의 요소들이 순서대로 따라오는 형태로 구성됩니다.
2,4) (1,3) 와 동일하지만, policy 에 따라 실행됩니다.
다음 모든 조건이 만족될 때만 이 오버로드들이 오버로드 해결에 참여합니다:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true 인 경우.

(C++20 이전)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true 인 경우.

(C++20 이후)

시퀀스의 원소들에 대한 이진 연산 binary_op 에 대한 일반화된 비가환 합 은 다음과 같이 정의됩니다:

  • 시퀀스에 요소가 하나만 있는 경우, 합계는 해당 요소의 값입니다.
  • 그렇지 않으면 다음 작업을 순서대로 수행합니다:
  1. 시퀀스에서 인접한 두 요소 elem1 elem2 를 선택합니다.
  2. binary_op ( elem1, elem2 ) 를 계산하고 시퀀스 내 두 요소를 결과로 대체합니다.
  3. 시퀀스에 요소가 하나만 남을 때까지 1단계와 2단계를 반복합니다.


binary_op 이 결합 법칙을 따르지 않는 경우(예: 부동 소수점 덧셈), 결과는 비결정적입니다.

오버로드 (1,2) 에 대해, binary_op ( unary_op ( * first ) , unary_op ( * first ) ) value type 으로 변환 가능하지 않으면, decltype ( first ) 의 프로그램은 형식이 잘못되었습니다.

오버로드 (3,4) 의 경우, 다음 값들 중 어느 하나라도 T 로 변환할 수 없으면 프로그램은 잘못된 형식입니다:

  • binary_op ( init, init )
  • binary_op ( init, unary_op ( * first ) )
  • binary_op ( unary_op ( * first ) , unary_op ( * first ) )
**참고:** 제공된 텍스트는 모두 C++ 코드 조각으로, ` ` 태그 내에 포함되어 있어 번역에서 제외됩니다. HTML 태그와 속성은 원본 형식을 유지하며, C++ 관련 용어(binary_op, unary_op, init, first 등)는 번역되지 않았습니다.

다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:

  • 오버로드 (1,2) 의 경우, decltype ( first ) 의 값 타입이 MoveConstructible 가 아닙니다.
  • 오버로드 (3,4) 의 경우, T MoveConstructible 가 아닙니다.
  • unary_op 또는 binary_op [ first , last ) 범위의 어떤 요소도 수정합니다.
  • unary_op 또는 binary_op [ first , last ] 범위의 어떤 반복자나 하위 범위를 무효화합니다.

목차

매개변수

first, last - 합계를 구할 요소들의 범위 를 정의하는 반복자 쌍
d_first - 대상 범위의 시작점; first 와 같을 수 있음
policy - 사용할 실행 정책
init - 초기값
unary_op - 입력 범위의 각 요소에 적용될 단항 FunctionObject . 반환 타입은 binary_op 의 입력으로 허용 가능해야 함
binary_op - unary_op 의 결과, 다른 binary_op 의 결과, 그리고 제공된 경우 init 에 적용될 이항 FunctionObject
타입 요구사항
-
InputIt LegacyInputIterator 요구사항을 충족해야 함
-
OutputIt LegacyOutputIterator 요구사항을 충족해야 함
-
ForwardIt1, ForwardIt2 LegacyForwardIterator 요구사항을 충족해야 함

반환값

쓰여진 마지막 요소 다음의 요소를 가리키는 반복자.

복잡도

주어진 N std:: distance ( first, last ) 인 경우:

1-4) O(N) 번의 unary_op binary_op 적용 각각.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.

참고 사항

unary_op init 에 적용되지 않습니다.

매개변수 init std::transform_exclusive_scan 과 달리 마지막에 나타나는데, 이는 이 함수에서는 선택적이기 때문입니다.

예제

#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
    std::vector data{3, 1, 4, 1, 5, 9, 2, 6};
    auto times_10 = [](int x) { return x * 10; };
    std::cout << "10 times exclusive sum: ";
    std::transform_exclusive_scan(data.begin(), data.end(),
                                  std::ostream_iterator<int>(std::cout, " "),
                                  0, std::plus<int>{}, times_10);
    std::cout << "\n10 times inclusive sum: ";
    std::transform_inclusive_scan(data.begin(), data.end(),
                                  std::ostream_iterator<int>(std::cout, " "),
                                  std::plus<int>{}, times_10);
    std::cout << '\n';
}

출력:

10 times exclusive sum: 0 30 40 80 90 140 230 250 
10 times inclusive sum: 30 40 80 90 140 230 250 310

참고 항목

요소 범위의 부분 합을 계산함
(함수 템플릿)
요소 범위에 함수를 적용하여 결과를 대상 범위에 저장함
(함수 템플릿)
std::partial_sum 와 유사하지만, i 번째 입력 요소를 i 번째 합계에 포함함
(함수 템플릿)
호출 가능 객체를 적용한 후 배타적 스캔을 계산함
(함수 템플릿)