std::inclusive_scan
From cppreference.com
| Defined in header <numeric>
|
||
template< class InputIt, class OutputIt >
OutputIt inclusive_scan( InputIt first, InputIt last,
OutputIt d_first );
|
(1) | (since C++17) (constexpr since C++20) |
template< class InputIt, class OutputIt, class BinaryOp >
OutputIt inclusive_scan( InputIt first, InputIt last,
OutputIt d_first, BinaryOp op );
|
(2) | (since C++17) (constexpr since C++20) |
template< class InputIt, class OutputIt,
class BinaryOp, class T >
OutputIt inclusive_scan( InputIt first, InputIt last,
OutputIt d_first, BinaryOp op, T init );
|
(3) | (since C++17) (constexpr since C++20) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2 >
ForwardIt2 inclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first );
|
(4) | (since C++17) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryOp >
ForwardIt2 inclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, BinaryOp op );
|
(5) | (since C++17) |
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) | (since C++17) |
Computes the inclusive prefix sums for all elements in the source range [first, last), the results are stored in the destination range [d_first, d_last), where d_last is std::next(d_first, std::distance(first, last)).
1) Equivalent to
inclusive_scan(first, last, d_first, std::plus<>()).2,3) For each iterator
iter in the source range and the corresponding iterator d_iter in the destination range, performs the following operations in order to calculate the corresponding generalized noncommutative sum including *iter:
- Creates an element sequence consisting of all elements of
[first,iter]in order. For overload (3), the sequence also consists ofinitas the first element. - If the sequence has two or more elements, replaces any two adjacent elements
e1ande2withop(e1, e2). - Repeats step 2 until the sequence only has one element.
- Writes the remaining element to
d_iter.
4-6) Same as (1-3), but executed according to
policy. These overloads participate in overload resolution only if the value of the following expression is
true:
|
|
(until C++20) |
|
|
(since C++20) |
Given add_op as the actual add operation:
- If one of the following conditions is satisfied, the program is ill-formed:
- For overloads (1,2,4,5),
add_op(*first, *first)is not convertible to the value type ofdecltype(first). - For overloads (3,6), any of the following values is not convertible to
T:add_op(init, init)add_op(init, *first)add_op(*first, *first)
- For overloads (1,2,4,5),
- If any of the following conditions is satisfied, the behavior is undefined:
- For overloads (1,2,4,5), the value type of
decltype(first)is not MoveConstructible. - For overloads (3,6),
Tis not MoveConstructible. add_opmodifies any element of the source range or the destination range.add_opinvalidates any iterator or subrange in[first,last]or[d_first,d_last].
- For overloads (1,2,4,5), the value type of
Parameters
| first, last | - | the pair of iterators defining the source range |
| d_first | - | the beginning of the destination range |
| init | - | the initial value |
| op | - | binary FunctionObject that will be applied in to the result of dereferencing the input iterators, the results of other op, and init (if provided)
|
| policy | - | the execution policy to use |
| Type requirements | ||
-InputIt must meet the requirements of LegacyInputIterator.
| ||
-OutputIt must meet the requirements of LegacyOutputIterator.
| ||
-ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.
| ||
Return value
The past-the-end iterator of the destination range (i.e. d_last).
Complexity
Given N as std::distance(first, last):
1,4) 𝓞(N) applications of
std::plus<>().2,3,5,6) 𝓞(N) applications of
op.Exceptions
4-6) During the execution process:
- If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
- If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).
Notes
The result is non-deterministic if the add_op is not associative (such as floating-point addition).
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
201603L |
(C++17) | Parallel algorithms, overloads (4-6) |
Example
Run this code
#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<>{});
}
Output:
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
See also
| computes the differences between adjacent elements in a range (function template) | |
| sums up or folds a range of elements (function template) | |
| computes the partial sum of a range of elements (function template) | |
(C++17) |
applies an invocable, then calculates inclusive scan (function template) |
(C++17) |
similar to std::partial_sum, excludes the ith input element from the ith sum (function template) |