Namespaces
Variants
Actions

std::transform_reduce

From cppreference.com
< cpp‎ | algorithm
 
 
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
(C++11)                (C++11)(C++11)

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
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
(C++17)
transform_reduce
(C++17)
Operations on uninitialized memory
 
 
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 ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class T >
T transform_reduce( ExecutionPolicy&& policy,
                    ForwardIt1 first1, ForwardIt1 last1,

                    ForwardIt2 first2, T init );
(2) (since C++17)
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 transform );
(3) (since C++17)
(constexpr since C++20)
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 transform );
(4) (since C++17)
template< class InputIt, class T,

          class BinaryOp, class UnaryOp >
T transform_reduce( InputIt first, InputIt last, T init,

                    BinaryOp reduce, UnaryOp transform );
(5) (since C++17)
(constexpr since C++20)
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 transform );
(6) (since C++17)
1) Equivalent to transform_reduce(first1, last1, first2, init,
                 std::plus<>(), std::multiplies<>())
, effectively parallelized version of the default std::inner_product.
3) Applies transform to each pair of elements from the ranges [first1last1) and the range of std::distance(first1, last1) elements starting from first2 and reduces the results (possibly permuted and aggregated in unspecified manner) along with the initial value init over reduce.
The result is non-deterministic if the reduce is not associative or not commutative (such as floating-point addition).
If any of the following values is not convertible to T, the program is ill-formed:
  • reduce(init, init)
  • reduce(init, transform(*first1, *first2))
  • reduce(transform(*first1, *first2), init)
  • reduce(transform(*first1, *first2), transform(*first1, *first2))
Given last2 as the std::distance(first1, last1)th next iterator of first2, if any of the following conditions is satisfied, the behavior is undefined:
  • T is not MoveConstructible.
  • transform or reduce modifies any element of [first1last1) or [first2last2).
  • transform or reduce invalidates any iterator or subrange of [first1last1] or [first2last2].
5) Applies transform to each element in the range [firstlast) and reduces the results (possibly permuted and aggregated in unspecified manner) along with the initial value init over reduce.
The result is non-deterministic if the reduce is not associative or not commutative (such as floating-point addition).
If any of the following values is not convertible to T, the program is ill-formed:
  • reduce(init, init)
  • reduce(init, transform(*first))
  • reduce(transform(*first), init)
  • reduce(transform(*first), transform(*first))
If any of the following conditions is satisfied, the behavior is undefined:
  • T is not MoveConstructible.
  • transform or reduce modifies any element of [firstlast).
  • transform or reduce invalidates any iterator or subrange of [firstlast].
2,4,6) Same as (1,3,5), but executed according to policy.
These overloads participate in overload resolution only if all following conditions are satisfied:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.

(until C++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.

(since C++20)

Contents

[edit] Parameters

first1, last1 - the pair of iterators defining the range of elements to be taken as the left operand of transform
first2 - the start of range of elements to be taken as the right operand of transform
first, last - the pair of iterators defining the range of elements to be taken as the operand of transform
init - the initial value of the generalized sum
policy - the execution policy to use
reduce - binary FunctionObject that will be applied in unspecified order to the results of transform, the results of other reduce and init.
transform - unary or binary FunctionObject that will be applied to each element of the input range(s). The return type must be acceptable as input to reduce.
Type requirements
-
InputIt1, InputIt2, InputIt must meet the requirements of