std::transform_reduce
| Defined in header <numeric>
|
||
template< class InputIt1, class InputIt2, class T >
T transform_reduce( InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init );
|
(1) | (since C++17) (constexpr since C++20) |
template< class InputIt1, class InputIt2, class T,
class BinaryOp1, class BinaryOp2 >
T transform_reduce( InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init,
BinaryOp1 reduce, BinaryOp2 binary_transform );
|
(2) | (since C++17) (constexpr since C++20) |
template< class InputIt, class T,
class BinaryOp, class UnaryOp >
T transform_reduce( InputIt first, InputIt last, T init,
BinaryOp reduce, UnaryOp unary_transform );
|
(3) | (since C++17) (constexpr since C++20) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class T >
T transform_reduce( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, T init );
|
(4) | (since C++17) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class T,
class BinaryOp1, class BinaryOp2 >
T transform_reduce( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, T init,
BinaryOp1 reduce, BinaryOp2 binary_transform );
|
(5) | (since C++17) |
template< class ExecutionPolicy,
class ForwardIt, class T,
class BinaryOp, class UnaryOp >
T transform_reduce( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, T init,
BinaryOp reduce, UnaryOp unary_transform );
|
(6) | (since C++17) |
Reduces the transformed target range(s), possibly permuted and aggregated in unspecified manner.
Calculates the generalized sum of init and a group of elements transformed from the target range(s), performs the following operations in order:
- Creates an element pool consisting of
initand all elements in the element group. - If the pool has two or more elements, picks any two of them and applies
reduce. Then adds the result to the element pool. - Repeats step 2 until the pool only has one element.
- Returns the remaining element.
transform_reduce(first1, last1, first2, init,
std::plus<>(), std::multiplies<>())
iter1 in the first target range [first1, last1) and the corresponding iterator iter2 in the second target range [first2, last2) (where last2 is std::next(std::distance(first1, last1))), adds binary_transform(*iter1, *iter2) to the element group.T, the program is ill-formed:
reduce(init, init)reduce(init, binary_transform(*first1, *first2))reduce(binary_transform(*first1, *first2), init)reduce(binary_transform(*first1, *first2), binary_transform(*first1, *first2))
Tis not MoveConstructible.binary_transformorreducemodifies any element of the two target ranges.binary_transformorreduceinvalidates any iterator or subrange of[first1,last1]or[first2,last2].
iter in the target range [first, last), adds unary_transform(*iter) to the element group.T, the program is ill-formed:
reduce(init, init)reduce(init, unary_transform(*first))reduce(unary_transform(*first), init)reduce(unary_transform(*first), unary_transform(*first))
Tis not MoveConstructible.transformorreducemodifies any element of the target range.transformorreduceinvalidates any iterator or subrange in[first,last].
policy.true:
|
|
(until C++20) |
|
|
(since C++20) |
Parameters
| first1, last1 | - | the pair of iterators defining the first target range |
| first2 | - | the beginning of the second target range |
| first, last | - | the pair of iterators defining the target range |
| init | - | the initial value of the generalized sum |
| reduce | - | binary FunctionObject that will be applied in unspecified order to the results of transform, the results of other reduce and init.
|
| binary_transform | - | binary FunctionObject that will be applied to each pair of corresponding elements of the two target ranges |
| unary_transform | - | unary FunctionObject that will be applied to each element of the target range |
| policy | - | the execution policy to use |
| Type requirements | ||
-InputIt1, InputIt2, InputIt must meet the requirements of LegacyInputIterator.
| ||
-ForwardIt1, ForwardIt2, ForwardIt must meet the requirements of LegacyForwardIterator.
| ||
Return value
As described above.
Complexity
Given N as std::distance(first1, last1) (or std::distance(first, last) for overloads (5,6)):
std::plus<>() and std::multiplies<>() respectively.reduce and transform respectively.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
binary_transform or unary_transform is never applied to init.
The result is non-deterministic if the reduce is not associative or not commutative (such as floating-point addition).
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
201603L |
(C++17) | Parallel algorithms, overloads (4-6) |
Example
transform_reduce can be used to parallelize std::inner_product. Some systems may need additional support to get advantages of parallel execution. E.g., on GNU/Linux, the Intel TBB be installed and -ltbb option be provided to gcc/clang compiler.
#if PARALLEL
#include <execution>
#define PAR std::execution::par,
#else
#define PAR
#endif
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <locale>
#include <numeric>
#include <vector>
// to parallelize non-associate accumulative operation, you'd better choose
// transform_reduce instead of reduce; e.g., a + b * b != b + a * a
void print_sum_squared(long const num)
{
std::cout.imbue(std::locale{"en_US.UTF8"});
std::cout << "num = " << num << '\n';
// create an immutable vector filled with pattern: 1,2,3,4, 1,2,3,4 ...
const std::vector<long> v{[n = num * 4]
{
std::vector<long> v;
v.reserve(n);
std::generate_n(std::back_inserter(v), n,
[i = 0]() mutable { return 1 + i++ % 4; });
return v;
}()};
auto squared_sum = [](auto sum, auto val) { return sum + val * val; };
auto sum1 = std::accumulate(v.cbegin(), v.cend(), 0L, squared_sum);
std::cout << "accumulate(): " << sum1 << '\n';
auto sum2 = std::reduce(PAR v.cbegin(), v.cend(), 0L, squared_sum);
std::cout << "reduce(): " << sum2 << '\n';
auto sum3 = std::transform_reduce(PAR v.cbegin(), v.cend(), 0L, std::plus{},
[](auto val) { return val * val; });
std::cout << "transform_reduce(): " << sum3 << "\n\n";
}
int main()
{
print_sum_squared(1);
print_sum_squared(1'000);
print_sum_squared(1'000'000);
}
Possible output:
num = 1
accumulate(): 30
reduce(): 30
transform_reduce(): 30
num = 1,000
accumulate(): 30,000
reduce(): -7,025,681,278,312,630,348
transform_reduce(): 30,000
num = 1,000,000
accumulate(): 30,000,000
reduce(): -5,314,886,882,370,003,032
transform_reduce(): 30,000,000
// Compile-options for parallel execution on POSIX:
// g++ -O2 -std=c++17 -Wall -Wextra -pedantic -DPARALLEL ./example.cpp -ltbb -o tr; ./tr
See also
| sums up or folds 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::accumulate, except out of order (function template) |