std::exclusive_scan
cppreference.net에서
| 헤더 <numeric>
|
||
template< class InputIt, class OutputIt, class T >
OutputIt exclusive_scan( InputIt first, InputIt last,
OutputIt d_first, T init );
|
에 정의됨 (1) | (C++17부터) (C++20부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class T >
ForwardIt2 exclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, T init );
|
(2) | (C++17부터) |
template< class InputIt, class OutputIt,
class T, class BinaryOp >
OutputIt exclusive_scan( InputIt first, InputIt last,
OutputIt d_first, T init, BinaryOp op );
|
(3) | (C++17부터) (C++20부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2,
class T, class BinaryOp >
ForwardIt2 exclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, T init, BinaryOp op );
|
(4) | (C++17부터) |
1)
exclusive_scan(first, last, d_first, init, std::plus<>()와 동일.3)
op을 사용하여 배타적 누적 합계를 계산합니다. 각 정수
i에 대해 [0, std::distance(first, last))에서 다음 연산을 순서대로 수행합니다:
- 다음으로 구성된 시퀀스를 생성합니다:
init뒤에[first,iter)의 요소들을 순서대로, 여기서iter은i번째 반복자first의 다음입니다. op에 대한 시퀀스의 일반화된 비가환 합을 계산합니다.- 결과를
*dest에 할당합니다. 여기서dest은i번째 반복자d_first의 다음입니다.
2,4) (1,3)과 동일하지만, 에 따라 실행됩니다.
policy 이 오버로드는 다음 표현식의 값이 인 경우에만 오버로드 해결에 참여합니다:
true(C++20까지)|
|
(C++20부터) |
|
|
이진 연산 |
에 대한 요소 시퀀스의 일반화된 비가환 합은 다음과 같이 정의됩니다:
binary_op시퀀스에 요소가 하나만 있으면 합은 해당 요소의 값입니다.
- 그렇지 않으면 다음 연산을 순서대로 수행합니다:
- 시퀀스에서 인접한 두 요소
- 와
elem1를 선택합니다.elem2 - 을 계산하고 시퀀스에서 두 요소를 결과로 대체합니다.
binary_op(elem1, elem2)시퀀스에 요소가 하나만 남을 때까지 1단계와 2단계를 반복합니다.
을 실제 이진 연산이라고 할 때:
binary_op
- 이 결합법칙을 따르지 않으면(예: 부동소수점 덧셈) 결과는 비결정적입니다.
binary_op다음 값 중 하나라도
- 으로 변환할 수 없으면 프로그램의 형식이 잘못되었습니다:
T다음 조건 중 하나라도 충족되면 동작이 정의되지 않습니다:
binary_op(init, *first)binary_op(init, init)binary_op(*first, *first)
T이(가) MoveConstructible이(가) 아님.binary_op[first,last)의 요소를 수정합니다.binary_op[first,last]의 반복자 또는 하위 범위를 무효화합니다.
매개변수
| first, last | - | 합산할 요소의 범위를 정의하는 반복자 쌍 |
| d_first | - | 대상 범위의 시작; first
|
| 와 같을 수 있음 | policy | - 사용할 실행 정책 |
| init | - | 초기값 |
| op | - | 입력 반복자를 역참조한 결과, 다른 의 결과, 에 적용될 이진 opFunctionObjectinit
|
| 형식 요구 사항 | ||
-InputIt은 LegacyInputIterator의 요구 사항을 충족해야 합니다.
| ||
-OutputIt은 LegacyOutputIterator의 요구 사항을 충족해야 합니다.
| ||
-ForwardIt1, ForwardIt2은 LegacyForwardIterator의 요구 사항을 충족해야 합니다.
| ||
반환값
마지막으로 작성된 요소 다음 요소를 가리키는 반복자.
복잡도
N이 std::distance(first, last)일 때:
1,2) O(N)번의
std::plus<>() 적용.3,4) O(N)번의
op 적용.예외
ExecutionPolicy라는 템플릿 매개변수가 있는 오버로드는 다음과 같이 오류를 보고합니다:
- 알고리즘의 일부로 호출된 함수가 예외를 발생시키고
ExecutionPolicy이(가) 표준 정책 중 하나인 경우 std::terminate가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현 정의입니다. - 알고리즘이 메모리 할당에 실패하면 std::bad_alloc이 발생합니다.
예제
이 코드 실행
#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
참조
| 범위 내 인접 요소 간의 차이를 계산합니다 (함수 템플릿) | |
| 범위 내 요소를 합산하거나 축소합니다 (함수 템플릿) | |
| 범위 내 요소의 부분 합을 계산합니다 (함수 템플릿) | |
(C++17) |
호출 가능 객체를 적용한 후 배타적 스캔을 계산합니다 (함수 템플릿) |
(C++17) |
std::partial_sum과 유사하며, i번째 입력 요소를 i번째 합에 포함합니다 (함수 템플릿) |