std::transform_inclusive_scan
| Defined in header <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) | (since C++17) (constexpr since C++20) |
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 );
|
(2) | (since C++17) (constexpr since C++20) |
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 );
|
(3) | (since C++17) |
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) | (since C++17) |
Computes the inclusive prefix sums for all elements (transformed by transform_op) 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)).
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
initfollowed by all elements (transformed bytransform_op) 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
e1ande2withadd_op(e1, e2). - Repeats step 2 until the sequence only has one element.
- Writes the remaining element to
d_iter.
policy.true:
|
|
(until C++20) |
|
|
(since C++20) |
If one of the following conditions is satisfied, the program is ill-formed:
- For overloads (1,3),
add_op(transform_op(*first), transform_op(*first))is not convertible to the value type ofdecltype(first). - For overloads (2,4), any of the following values is not convertible to
T:add_op(init, init)add_op(init, transform_op(*first))add_op(transform_op(*first), transform_op(*first))
If any of the following conditions is satisfied, the behavior is undefined:
- For overloads (1,3), the value type of
decltype(first)is not MoveConstructible. - For overloads (2,4),
Tis not MoveConstructible. add_oportransform_opmodifies any element of the source range or the destination range.add_oportransform_opinvalidates any iterator or subrange in[first,last]or[d_first,d_last].
Parameters
| first, last | - | the pair of iterators defining the source range |
| d_first | - | the beginning of the destination range |
| init | - | the initial value |
| add_op | - | binary FunctionObject that will be applied in to the result of transform_op, the results of other add_op, and init.
|
| transform_op | - | unary FunctionObject that will be applied to each element of the source range |
| 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):
add_op and transform_op.Exceptions
- 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
transform_op is never applied to init.
The result is non-deterministic if the add_op is not associative (such as floating-point addition).
The parameter init appears last, differing from std::transform_exclusive_scan, because it is optional for this function.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
201603L |
(C++17) | Parallel algorithms, overloads (3,4) |
Example
#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';
}
Output:
10 times exclusive sum: 0 30 40 80 90 140 230 250
10 times inclusive sum: 30 40 80 90 140 230 250 310
See also
| computes the partial sum of a range of elements (function template) | |
| applies a function to a range of elements, storing results in a destination range (function template & algorithm function object) | |
(C++20) |
|
(C++17) |
similar to std::partial_sum, includes the ith input element in the ith sum (function template) |
(C++17) |
applies an invocable, then calculates exclusive scan (function template) |