Namespaces
Variants

std:: 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
Operations on uninitialized memory
헤더 파일에 정의됨 <numeric>
template < class InputIt, class OutputIt >

OutputIt inclusive_scan ( InputIt first, InputIt last,

OutputIt d_first ) ;
(1) (C++17부터)
(C++20부터 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2 >
ForwardIt2 inclusive_scan ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first ) ;
(2) (C++17부터)
template < class InputIt, class OutputIt, class BinaryOp >

OutputIt inclusive_scan ( InputIt first, InputIt last,

OutputIt d_first, BinaryOp op ) ;
(3) (C++17부터)
(C++20부터 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2, class BinaryOp >
ForwardIt2 inclusive_scan ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, BinaryOp op ) ;
(4) (C++17부터)
template < class InputIt, class OutputIt,

class BinaryOp, class T >
OutputIt inclusive_scan ( InputIt first, InputIt last,

OutputIt d_first, BinaryOp op, T init ) ;
(5) (C++17부터)
(C++20부터 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2,
class BinaryOp, class T >
ForwardIt2 inclusive_scan ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, BinaryOp op, T init ) ;
(6) (C++17부터)
1) inclusive_scan ( first, last, d_first, std:: plus <> ( ) 와 동등합니다.
3) op 을 사용하여 포함적 접두사 합을 계산합니다.
각 정수 i 에 대해 [ 0 , std:: distance ( first, last ) ) 범위 내에서, 다음 연산들을 순서대로 수행합니다:
  1. [ first , iter ] 범위의 요소들로 구성된 시퀀스를 생성합니다. 여기서 iter first 의 다음 i 번째 반복자입니다.
  2. op 를 사용하여 시퀀스의 일반화된 비가환 합을 계산합니다.
  3. 결과를 * dest 에 할당합니다. 여기서 dest d_first 의 다음 i 번째 반복자입니다.
5) (3) 와 동일하지만, 생성된 각 시퀀스는 init 뒤에 [ first , iter ] 범위의 요소들이 순서대로 따라오는 형태로 구성됩니다.
2,4,6) (1,3,5) 와 동일하지만, 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 가 실제 이항 연산인 경우:

  • 결과는 binary_op 이 결합 법칙을 만족하지 않는 경우(예: 부동 소수점 덧셈) 비결정적입니다.
  • 오버로드 (1-4) 의 경우, binary_op ( * first, * first ) decltype ( first ) value type 으로 변환 가능하지 않으면 프로그램의 형식이 잘못되었습니다.
  • 오버로드 (5,6) 의 경우, 다음 값들 중 어느 하나라도 T 로 변환할 수 없으면 프로그램의 형식이 올바르지 않습니다:
  • binary_op ( init, * first )
  • binary_op ( init, init )
  • binary_op ( * first, * first )
  • 다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:
  • 오버로드 (1-4) 의 경우, decltype ( first ) 의 값 타입이 MoveConstructible 가 아닙니다.
  • 오버로드 (5,6) 의 경우, T MoveConstructible 가 아닙니다.
  • binary_op [ first , last ) 범위의 어떤 요소를 수정합니다.
  • binary_op [ first , last ] 범위의 어떤 반복자나 하위 범위를 무효화합니다.

목차

매개변수

first, last - 합산할 요소들의 소스 범위 를 정의하는 반복자 쌍
d_first - 대상 범위 의 시작점; first 와 같을 수 있음
policy - 사용할 실행 정책
init - 초기값
op - 입력 반복자를 역참조한 결과, 다른 op 의 결과, 그리고 init (제공된 경우)에 적용될 이항 FunctionObject
타입 요구사항
-
InputIt LegacyInputIterator 요구사항을 충족해야 함
-
OutputIt LegacyOutputIterator 요구사항을 충족해야 함
-
ForwardIt1, ForwardIt2 LegacyForwardIterator 요구사항을 충족해야 함

반환값

마지막으로 기록된 요소의 다음 요소를 가리키는 반복자.

복잡도

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

1,2) O(N) 번의 std:: plus <> ( ) 적용.
3-6) O(N) 번의 op 적용.

예외

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

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

예제

#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
    std::vector data{3, 1, 4, 1, 5, 9, 2, 6};
    std::cout << "Exclusive sum: ";
    std::exclusive_scan(data.begin(), data.end(),
                        std::ostream_iterator<int>(std::cout, " "),
                        0);
    std::cout << "\nInclusive sum: ";
    std::inclusive_scan(data.begin(), data.end(),
                        std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\n\nExclusive product: ";
    std::exclusive_scan(data.begin(), data.end(),
                        std::ostream_iterator<int>(std::cout, " "),
                        1, std::multiplies<>{});
    std::cout << "\nInclusive product: ";
    std::inclusive_scan(data.begin(), data.end(),
                        std::ostream_iterator<int>(std::cout, " "),
                        std::multiplies<>{});
}

출력:

Exclusive sum: 0 3 4 8 9 14 23 25
Inclusive sum: 3 4 8 9 14 23 25 31
Exclusive product: 1 3 3 12 12 60 540 1080
Inclusive product: 3 3 12 12 60 540 1080 6480

참고 항목

범위 내 인접한 요소들 간의 차이를 계산합니다
(function template)
요소들의 범위를 합산하거나 접습니다
(function template)
요소 범위의 부분 합을 계산합니다
(function template)
호출 가능 객체를 적용한 후, inclusive scan을 계산합니다
(function template)
std::partial_sum 과 유사하지만, i 번째 입력 요소를 i 번째 합계에서 제외합니다
(function template)