[transform.reduce]

# 26 Algorithms library [[algorithms]](./#algorithms)

## 26.10 Generalized numeric operations [[numeric.ops]](numeric.ops#transform.reduce)

### 26.10.6 Transform reduce [transform.reduce]

[ð](#lib:transform_reduce)

`template<class InputIterator1, class InputIterator2, class T>
  constexpr T transform_reduce(InputIterator1 first1, InputIterator1 last1,
                               InputIterator2 first2,
                               T init);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12504)

*Effects*: Equivalent to:return transform_reduce(first1, last1, first2, init, plus<>(), multiplies<>());

[ð](#lib:transform_reduce_)

`template<class ExecutionPolicy,
         class ForwardIterator1, class ForwardIterator2, class T>
  T transform_reduce(ExecutionPolicy&& exec,
                     ForwardIterator1 first1, ForwardIterator1 last1,
                     ForwardIterator2 first2,
                     T init);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12523)

*Effects*: Equivalent to:return transform_reduce(std::forward<ExecutionPolicy>(exec),
 first1, last1, first2, init, plus<>(), multiplies<>());

[ð](#lib:transform_reduce__)

`template<class InputIterator1, class InputIterator2, class T,
         class BinaryOperation1, class BinaryOperation2>
  constexpr T transform_reduce(InputIterator1 first1, InputIterator1 last1,
                               InputIterator2 first2,
                               T init,
                               BinaryOperation1 binary_op1,
                               BinaryOperation2 binary_op2);
template<class ExecutionPolicy,
         class ForwardIterator1, class ForwardIterator2, class T,
         class BinaryOperation1, class BinaryOperation2>
  T transform_reduce(ExecutionPolicy&& exec,
                     ForwardIterator1 first1, ForwardIterator1 last1,
                     ForwardIterator2 first2,
                     T init,
                     BinaryOperation1 binary_op1,
                     BinaryOperation2 binary_op2);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12553)

*Mandates*: All of 

- [(3.1)](#3.1)

binary_op1(init, init),

- [(3.2)](#3.2)

binary_op1(init, binary_op2(*first1, *first2)),

- [(3.3)](#3.3)

binary_op1(binary_op2(*first1, *first2), init), and

- [(3.4)](#3.4)

binary_op1(binary_op2(*first1, *first2), binary_op2(*first1, *first2))

are convertible to T[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12564)

*Preconditions*: 

- [(4.1)](#4.1)

  T meets the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") (Table [31](utility.arg.requirements#tab:cpp17.moveconstructible "Table 31: Cpp17MoveConstructible requirements")) requirements[.](#4.1.sentence-1)

- [(4.2)](#4.2)

  Neither binary_op1 nor binary_op2 invalidates subranges, nor modifies elements in the ranges
 [first1, last1] and [first2, first2 + (last1 - first1)][.](#4.2.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12575)

*Returns*: *GENERALIZED_SUM*(binary_op1, init, binary_op2(*i, *(first2 + (i - first1))), …) for every iterator i in [first1, last1)[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12582)

*Complexity*: O(last1 - first1) applications each
of binary_op1 and binary_op2[.](#6.sentence-1)

[ð](#lib:transform_reduce___)

`template<class InputIterator, class T,
         class BinaryOperation, class UnaryOperation>
  constexpr T transform_reduce(InputIterator first, InputIterator last, T init,
                               BinaryOperation binary_op, UnaryOperation unary_op);
template<class ExecutionPolicy,
         class ForwardIterator, class T,
         class BinaryOperation, class UnaryOperation>
  T transform_reduce(ExecutionPolicy&& exec,
                     ForwardIterator first, ForwardIterator last,
                     T init, BinaryOperation binary_op, UnaryOperation unary_op);
`

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12603)

*Mandates*: All of 

- [(7.1)](#7.1)

binary_op(init, init),

- [(7.2)](#7.2)

binary_op(init, unary_op(*first)),

- [(7.3)](#7.3)

binary_op(unary_op(*first), init), and

- [(7.4)](#7.4)

binary_op(unary_op(*first), unary_op(*first))

are convertible to T[.](#7.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12614)

*Preconditions*: 

- [(8.1)](#8.1)

  T meets the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") (Table [31](utility.arg.requirements#tab:cpp17.moveconstructible "Table 31: Cpp17MoveConstructible requirements")) requirements[.](#8.1.sentence-1)

- [(8.2)](#8.2)

  Neither unary_op nor binary_op invalidates subranges,
 nor modifies elements in the range [first, last][.](#8.2.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12624)

*Returns*: *GENERALIZED_SUM*(binary_op, init, unary_op(*i), …) for every iterator i in [first, last)[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12631)

*Complexity*: O(last - first) applications each of unary_op andbinary_op[.](#10.sentence-1)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12636)

[*Note [1](#note-1)*:

transform_reduce does not apply unary_op to init[.](#11.sentence-1)

â *end note*]
