[reduce]

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

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

### 26.10.4 Reduce [reduce]

[ð](#lib:reduce)

`template<class InputIterator>
  constexpr typename iterator_traits<InputIterator>::value_type
    reduce(InputIterator first, InputIterator last);
`

[1](#1)

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

*Effects*: Equivalent to:return reduce(first, last, typename iterator_traits<InputIterator>::value_type{});

[ð](#lib:reduce_)

`template<class ExecutionPolicy, class ForwardIterator>
  typename iterator_traits<ForwardIterator>::value_type
    reduce(ExecutionPolicy&& exec,
           ForwardIterator first, ForwardIterator last);
`

[2](#2)

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

*Effects*: Equivalent to:return reduce(std::forward<ExecutionPolicy>(exec), first, last, typename iterator_traits<ForwardIterator>::value_type{});

[ð](#lib:reduce__)

`template<class InputIterator, class T>
  constexpr T reduce(InputIterator first, InputIterator last, T init);
`

[3](#3)

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

*Effects*: Equivalent to:return reduce(first, last, init, plus<>());

[ð](#lib:reduce___)

`template<class ExecutionPolicy, class ForwardIterator, class T>
  T reduce(ExecutionPolicy&& exec,
           ForwardIterator first, ForwardIterator last, T init);
`

[4](#4)

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

*Effects*: Equivalent to:return reduce(std::forward<ExecutionPolicy>(exec), first, last, init, plus<>());

[ð](#lib:reduce____)

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

[5](#5)

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

*Mandates*: All of

- [(5.1)](#5.1)

binary_op(init, *first),

- [(5.2)](#5.2)

binary_op(*first, init),

- [(5.3)](#5.3)

binary_op(init, init), and

- [(5.4)](#5.4)

binary_op(*first, *first)

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

[6](#6)

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

*Preconditions*: 

- [(6.1)](#6.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[.](#6.1.sentence-1)

- [(6.2)](#6.2)

  binary_op neither invalidates iterators or subranges,
 nor modifies elements in the range [first, last][.](#6.2.sentence-1)

[7](#7)

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

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

[8](#8)

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

*Complexity*: O(last - first) applications of binary_op[.](#8.sentence-1)

[9](#9)

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

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

The difference between reduce and accumulate is thatreduce applies binary_op in an unspecified order,
which yields a nondeterministic result
for non-associative or non-commutative binary_op such as floating-point addition[.](#9.sentence-1)

â *end note*]
