26 Numerics library [numerics]

26.8 Generalized numeric operations [numeric.ops]

26.8.1 Header <numeric> synopsis [numeric.ops.overview]

namespace std {
  template <class InputIterator, class T>
    T accumulate(InputIterator first, InputIterator last, T init);
  template <class InputIterator, class T, class BinaryOperation>
    T accumulate(InputIterator first, InputIterator last, T init,
                 BinaryOperation binary_op);

  template<class InputIterator>
    typename iterator_traits<InputIterator>::value_type
      reduce(InputIterator first, InputIterator last);
  template<class InputIterator, class T>
    T reduce(InputIterator first, InputIterator last, T init);
  template<class InputIterator, class T, class BinaryOperation>
    T reduce(InputIterator first, InputIterator last, T init,
             BinaryOperation binary_op);
  template<class ExecutionPolicy, class InputIterator>
    typename iterator_traits<InputIterator>::value_type
      reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
             InputIterator first, InputIterator last);
  template<class ExecutionPolicy, class InputIterator, class T>
    T reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
             InputIterator first, InputIterator last, T init);
  template<class ExecutionPolicy, class InputIterator, class T, class BinaryOperation>
    T reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
             InputIterator first, InputIterator last, T init,
             BinaryOperation binary_op);

  template<class InputIterator, class UnaryFunction, class T, class BinaryOperation>
    T transform_reduce(InputIterator first, InputIterator last,
                       UnaryOperation unary_op, T init, BinaryOperation binary_op);
  template<class ExecutionPolicy, class InputIterator,
           class UnaryFunction, class T, class BinaryOperation>
    T transform_reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                       InputIterator first, InputIterator last,
                       UnaryOperation unary_op, T init, BinaryOperation binary_op);

  template <class InputIterator1, class InputIterator2, class T>
    T inner_product(InputIterator1 first1, InputIterator1 last1,
                    InputIterator2 first2, T init);
  template <class InputIterator1, class InputIterator2, class T,
            class BinaryOperation1, class BinaryOperation2>
    T inner_product(InputIterator1 first1, InputIterator1 last1,
                    InputIterator2 first2, T init,
                    BinaryOperation1 binary_op1,
                    BinaryOperation2 binary_op2);
  template <class ExecutionPolicy, class InputIterator1, class InputIterator2,
            class T>
    T inner_product(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                    InputIterator1 first1, InputIterator1 last1,
                    InputIterator2 first2, T init);
  template <class ExecutionPolicy, class InputIterator1, class InputIterator2,
            class T, class BinaryOperation1, class BinaryOperation2>
    T inner_product(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                    InputIterator1 first1, InputIterator1 last1,
                    InputIterator2 first2, T init,
                    BinaryOperation1 binary_op1,
                    BinaryOperation2 binary_op2);

  template <class InputIterator, class OutputIterator>
    OutputIterator partial_sum(InputIterator first,
                               InputIterator last,
                               OutputIterator result);
  template <class InputIterator, class OutputIterator, class BinaryOperation>
    OutputIterator partial_sum(InputIterator first,
                               InputIterator last,
                               OutputIterator result,
                               BinaryOperation binary_op);

  template<class InputIterator, class OutputIterator, class T>
    OutputIterator exclusive_scan(InputIterator first, InputIterator last,
                                  OutputIterator result,
                                  T init);
  template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
    OutputIterator exclusive_scan(InputIterator first, InputIterator last,
                                  OutputIterator result,
                                  T init, BinaryOperation binary_op);
  template<class ExecutionPolicy, class InputIterator, class OutputIterator, class T>
    OutputIterator exclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                  InputIterator first, InputIterator last,
                                  OutputIterator result,
                                  T init);
  template<class ExecutionPolicy, class InputIterator, class OutputIterator, class T,
           class BinaryOperation>
    OutputIterator exclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                  InputIterator first, InputIterator last,
                                  OutputIterator result,
                                  T init, BinaryOperation binary_op);
  template<class InputIterator, class OutputIterator>
    OutputIterator inclusive_scan(InputIterator first, InputIterator last,
                                  OutputIterator result);
  template<class InputIterator, class OutputIterator, class BinaryOperation>
    OutputIterator inclusive_scan(InputIterator first, InputIterator last,
                                  OutputIterator result,
                                  BinaryOperation binary_op);
  template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
    OutputIterator inclusive_scan(InputIterator first, InputIterator last,
                                  OutputIterator result,
                                  BinaryOperation binary_op, T init);
  template<class ExecutionPolicy, class InputIterator, class OutputIterator>
    OutputIterator inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                  InputIterator first, InputIterator last,
                                  OutputIterator result);
  template<class ExecutionPolicy, class InputIterator, class OutputIterator,
           class BinaryOperation>
    OutputIterator inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                  InputIterator first, InputIterator last,
                                  OutputIterator result,
                                  BinaryOperation binary_op);
  template<class ExecutionPolicy, class InputIterator, class OutputIterator,
           class BinaryOperation, class T>
    OutputIterator inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                  InputIterator first, InputIterator last,
                                  OutputIterator result,
                                  BinaryOperation binary_op, T init);

  template<class InputIterator, class OutputIterator,
           class UnaryOperation,
           class T, class BinaryOperation>
    OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last,
                                            OutputIterator result,
                                            UnaryOperation unary_op,
                                            T init, BinaryOperation binary_op);
  template<class ExecutionPolicy, class InputIterator, class OutputIterator,
           class UnaryOperation,
           class T, class BinaryOperation>
    OutputIterator transform_exclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                            InputIterator first, InputIterator last,
                                            OutputIterator result,
                                            UnaryOperation unary_op,
                                            T init, BinaryOperation binary_op);
  template<class InputIterator, class OutputIterator,
           class UnaryOperation,
           class BinaryOperation>
  OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
                                          OutputIterator result,
                                          UnaryOperation unary_op,
                                          BinaryOperation binary_op);
  template<class InputIterator, class OutputIterator,
           class UnaryOperation,
           class BinaryOperation, class T>
  OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
                                          OutputIterator result,
                                          UnaryOperation unary_op,
                                          BinaryOperation binary_op, T init);
  template<class ExecutionPolicy, class InputIterator, class OutputIterator,
           class UnaryOperation,
           class BinaryOperation>
  OutputIterator transform_inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                          InputIterator first, InputIterator last,
                                          OutputIterator result,
                                          UnaryOperation unary_op,
                                          BinaryOperation binary_op);
  template<class ExecutionPolicy, class InputIterator, class OutputIterator,
           class UnaryOperation,
           class BinaryOperation, class T>
  OutputIterator transform_inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                          InputIterator first, InputIterator last,
                                          OutputIterator result,
                                          UnaryOperation unary_op,
                                          BinaryOperation binary_op, T init);

  template <class InputIterator, class OutputIterator>
    OutputIterator adjacent_difference(InputIterator first,
                                       InputIterator last,
                                       OutputIterator result);
  template <class InputIterator, class OutputIterator, class BinaryOperation>
    OutputIterator adjacent_difference(InputIterator first,
                                       InputIterator last,
                                       OutputIterator result,
                                       BinaryOperation binary_op);
  template <class ExecutionPolicy, class InputIterator, class OutputIterator>
    OutputIterator adjacent_difference(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                       InputIterator first,
                                       InputIterator last,
                                       OutputIterator result);
  template <class ExecutionPolicy, class InputIterator, class OutputIterator,
            class BinaryOperation>
    OutputIterator adjacent_difference(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
                                       InputIterator first,
                                       InputIterator last,
                                       OutputIterator result,
                                       BinaryOperation binary_op);

  template <class ForwardIterator, class T>
    void iota(ForwardIterator first, ForwardIterator last, T value);

  // [numeric.ops.gcd], greatest common divisor
  template <class M, class N>
    constexpr common_type_t<M,N> gcd(M m, N n);

  // [numeric.ops.lcm], least common multiple
  template <class M, class N>
    constexpr common_type_t<M,N> lcm(M m, N n);
}

The requirements on the types of algorithms' arguments that are described in the introduction to Clause [algorithms] also apply to the following algorithms.

26.8.2 Accumulate [accumulate]

template <class InputIterator, class T> T accumulate(InputIterator first, InputIterator last, T init); template <class InputIterator, class T, class BinaryOperation> T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);

Requires: T shall meet the requirements of CopyConstructible (Table [tab:copyconstructible]) and CopyAssignable (Table [tab:copyassignable]) types. In the range [first, last], binary_op shall neither modify elements nor invalidate iterators or subranges.284

Effects: Computes its result by initializing the accumulator acc with the initial value init and then modifies it with acc = acc + *i or acc = binary_op(acc, *i) for every iterator i in the range [first, last) in order.285

The use of fully closed ranges is intentional.

accumulate is similar to the APL reduction operator and Common Lisp reduce function, but it avoids the difficulty of defining the result of reduction on an empty sequence by always requiring an initial value.

26.8.3 Reduce [reduce]

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

Effects: Equivalent to:

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

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

Effects: Equivalent to:

return reduce(std::forward<ExecutionPolicy>(exec), first, last,
              typename iterator_traits<InputIterator>::value_type{});

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

Effects: Equivalent to:

return reduce(first, last, init, plus<>());

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

Effects: Equivalent to:

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

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

Requires: binary_op shall neither invalidate iterators or subranges, nor modify elements in the range [first, last).

Returns: GENERALIZED_SUM(binary_op, init, *i, ...) for every i in [first, last).

Complexity: Ο(last - first) applications of binary_op.

Notes: The difference between reduce and accumulate is that reduce 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.

26.8.4 Transform reduce [transform.reduce]

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

Requires: Neither unary_op nor binary_op shall invalidate subranges, or modify elements in the range [first, last).

Returns: GENERALIZED_SUM(binary_op, init, unary_op(*i), ...) for every i in [first, last).

Complexity: Ο(last - first) applications each of unary_op and binary_op.

Notes: transform_reduce does not apply unary_op to init.

26.8.5 Inner product [inner.product]

template <class InputIterator1, class InputIterator2, class T> T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init); template <class ExecutionPolicy, class InputIterator1, class InputIterator2, class T> T inner_product(ExecutionPolicy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init); template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); template <class ExecutionPolicy, class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> T inner_product(ExecutionPolicy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);

Requires: T shall meet the requirements of CopyConstructible (Table [tab:copyconstructible]) and CopyAssignable (Table [tab:copyassignable]) types. In the ranges [first1, last1] and [first2, first2 + (last1 - first1)] binary_op1 and binary_op2 shall neither modify elements nor invalidate iterators or subranges.286

Effects: Computes its result by initializing the accumulator acc with the initial value init and then modifying it with acc = acc + (*i1) * (*i2) or acc = binary_op1(acc, binary_op2(*i1, *i2)) for every iterator i1 in the range [first1, last1) and iterator i2 in the range [first2, first2 + (last1 - first1)) in order.

The use of fully closed ranges is intentional.

26.8.6 Partial sum [partial.sum]

template <class InputIterator, class OutputIterator> OutputIterator partial_sum( InputIterator first, InputIterator last, OutputIterator result); template <class InputIterator, class OutputIterator, class BinaryOperation> OutputIterator partial_sum( InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);

Requires: InputIterator's value type shall be constructible from the type of *first. The result of the expression acc + *i or binary_op(acc, *i) shall be implicitly convertible to InputIterator's value type. acc shall be writable ([iterator.requirements.general]) to the result output iterator. In the ranges [first, last] and [result, result + (last - first)] binary_op shall neither modify elements nor invalidate iterators or subranges.287

Effects: For a non-empty range, the function creates an accumulator acc whose type is InputIterator's value type, initializes it with *first, and assigns the result to *result. For every iterator i in [first + 1, last) in order, acc is then modified by acc = acc + *i or acc = binary_op(acc, *i) and the result is assigned to *(result + (i - first)).

Returns: result + (last - first).

Complexity: Exactly (last - first) - 1 applications of the binary operation.

Remarks: result may be equal to first.

The use of fully closed ranges is intentional.

26.8.7 Exclusive scan [exclusive.scan]

template<class InputIterator, class OutputIterator, class T> OutputIterator exclusive_scan(InputIterator first, InputIterator last, OutputIterator result, T init);

Effects: Equivalent to:

return exclusive_scan(first, last, result, init, plus<>());

template<class ExecutionPolicy, class InputIterator, class OutputIterator, class T> OutputIterator exclusive_scan(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result, T init);

Effects: Equivalent to:

return exclusive_scan(std::forward<ExecutionPolicy>(exec), first, last, result, init, plus<>());

template<class InputIterator, class OutputIterator, class T, class BinaryOperation> OutputIterator exclusive_scan(InputIterator first, InputIterator last, OutputIterator result, T init, BinaryOperation binary_op); template<class ExecutionPolicy, class InputIterator, class OutputIterator, class T, class BinaryOperation> OutputIterator exclusive_scan(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result, T init, BinaryOperation binary_op);

Requires: binary_op shall neither invalidate iterators or subranges, nor modify elements in the ranges [first, last) or [result, result + (last - first)).

Effects: Assigns through each iterator i in [result, result + (last - first)) the value of GENERALIZED_NONCOMMUTATIVE_SUM(binary_op, init, *j, ...) for every j in [first, first + (i - result)).

Returns: The end of the resulting range beginning at result.

Complexity: Ο(last - first) applications of binary_op.

Remarks: result may be equal to first.

Notes: The difference between exclusive_scan and inclusive_scan is that exclusive_scan excludes the ith input element from the ith sum. If binary_op is not mathematically associative, the behavior of exclusive_scan may be nondeterministic.

26.8.8 Inclusive scan [inclusive.scan]

template<class InputIterator, class OutputIterator> OutputIterator inclusive_scan(InputIterator first, InputIterator last, OutputIterator result);

Effects: Equivalent to:

return inclusive_scan(first, last, result, plus<>());

template<class ExecutionPolicy, class InputIterator, class OutputIterator> OutputIterator inclusive_scan(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result);

Effects: Equivalent to:

return inclusive_scan(std::forward<ExecutionPolicy>(exec), first, last, result, plus<>());

template<class InputIterator, class OutputIterator, class BinaryOperation> OutputIterator inclusive_scan(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); template<class ExecutionPolicy, class InputIterator, class OutputIterator, class BinaryOperation> OutputIterator inclusive_scan(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); template<class InputIterator, class OutputIterator, class BinaryOperation, class T> OutputIterator inclusive_scan(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op, T init); template<class ExecutionPolicy, class InputIterator, class OutputIterator, class BinaryOperation, class T> OutputIterator inclusive_scan(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op, T init);

Requires: binary_op shall not invalidate iterators or subranges, nor modify elements in the ranges [first, last) or [result, result + (last - first)).

Effects: Assigns through each iterator i in [result, result + (last - first)) the value of

  • GENERALIZED_NONCOMMUTATIVE_SUM(binary_op, init, *j, ...) for every j in [first, first + (i - result + 1)) if init is provided, or

  • GENERALIZED_NONCOMMUTATIVE_SUM(binary_op, *j, ...) for every j in [first, first + (i - result + 1)) otherwise.

Returns: The end of the resulting range beginning at result.

Complexity: Ο(last - first) applications of binary_op.

Remarks: result may be equal to first.

Notes: The difference between exclusive_scan and inclusive_scan is that inclusive_scan includes the ith input element in the ith sum. If binary_op is not mathematically associative, the behavior of inclusive_scan may be nondeterministic.

26.8.9 Transform exclusive scan [transform.exclusive.scan]

template<class InputIterator, class OutputIterator, class UnaryOperation, class T, class BinaryOperation> OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op, T init, BinaryOperation binary_op); template<class ExecutionPolicy, class InputIterator, class OutputIterator, class UnaryOperation, class T, class BinaryOperation> OutputIterator transform_exclusive_scan(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op, T init, BinaryOperation binary_op);

Requires: Neither unary_op nor binary_op shall invalidate iterators or subranges, or modify elements in the ranges [first, last) or [result, result + (last - first)).

Effects: Assigns through each iterator i in [result, result + (last - first)) the value of GENERALIZED_NONCOMMUTATIVE_SUM(binary_op, init, unary_op(*j), ...) for every j in [first, first + (i - result)).

Returns: The end of the resulting range beginning at result.

Complexity: Ο(last - first) applications each of unary_op and binary_op.

Remarks: result may be equal to first.

Notes: The difference between transform_exclusive_scan and transform_inclusive_scan is that transform_exclusive_scan excludes the ith input element from the ith sum. If binary_op is not mathematically associative, the behavior of transform_exclusive_scan may be nondeterministic. transform_exclusive_scan does not apply unary_op to init.

26.8.10 Transform inclusive scan [transform.inclusive.scan]

template<class InputIterator, class OutputIterator, class UnaryOperation, class BinaryOperation> OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op, BinaryOperation binary_op); template<class ExecutionPolicy, class InputIterator, class OutputIterator, class UnaryOperation, class BinaryOperation> OutputIterator transform_inclusive_scan(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op, BinaryOperation binary_op); template<class InputIterator, class OutputIterator, class UnaryOperation, class BinaryOperation, class T> OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op, BinaryOperation binary_op, T init); template<class ExecutionPolicy, class InputIterator, class OutputIterator, class UnaryOperation, class BinaryOperation, class T> OutputIterator transform_inclusive_scan(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op, BinaryOperation binary_op, T init);

Requires: Neither unary_op nor binary_op shall invalidate iterators or subranges, or modify elements in the ranges [first, last) or [result, result + (last - first)).

Effects: Assigns through each iterator i in [result, result + (last - first)) the value of

  • GENERALIZED_NONCOMMUTATIVE_SUM(binary_op, init, unary_op(*j), ...) for every j in [first, first + (i - result + 1)) if init is provided, or

  • GENERALIZED_NONCOMMUTATIVE_SUM(binary_op, unary_op(*j), ...) for every j in [first, first + (i - result + 1)) otherwise.

Returns: The end of the resulting range beginning at result.

Complexity: Ο(last - first) applications each of unary_op and binary_op.

Remarks: result may be equal to first.

Notes: The difference between transform_exclusive_scan and transform_inclusive_scan is that transform_inclusive_scan includes the ith input element in the ith sum. If binary_op is not mathematically associative, the behavior of transform_inclusive_scan may be nondeterministic. transform_inclusive_scan does not apply unary_op to init.

26.8.11 Adjacent difference [adjacent.difference]

template <class InputIterator, class OutputIterator> OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result); template <class ExecutionPolicy, class InputIterator, class OutputIterator> OutputIterator adjacent_difference(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result); template <class InputIterator, class OutputIterator, class BinaryOperation> OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); template <class ExecutionPolicy, class InputIterator, class OutputIterator, class BinaryOperation> OutputIterator adjacent_difference(ExecutionPolicy&& exec, InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);

Requires: InputIterator's value type shall be MoveAssignable (Table [tab:moveassignable]) and shall be constructible from the type of *first. acc shall be writable ([iterator.requirements.general]) to the result output iterator. The result of the expression val - acc or binary_op(val, acc) shall be writable to the result output iterator. In the ranges [first, last] and [result, result + (last - first)], binary_op shall neither modify elements nor invalidate iterators or subranges.288

Effects: For a non-empty range, the function creates an accumulator acc whose type is InputIterator's value type, initializes it with *first, and assigns the result to *result. For every iterator i in [first + 1, last) in order, creates an object val whose type is InputIterator's value type, initializes it with *i, computes val - acc or binary_op(val, acc), assigns the result to *(result + (i - first)), and move assigns from val to acc.

Returns: result + (last - first).

Complexity: Exactly (last - first) - 1 applications of the binary operation.

Remarks: result may be equal to first.

The use of fully closed ranges is intentional.

26.8.12 Iota [numeric.iota]

template <class ForwardIterator, class T> void iota(ForwardIterator first, ForwardIterator last, T value);

Requires: T shall be convertible to ForwardIterator's value type. The expression ++val, where val has type T, shall be well formed.

Effects: For each element referred to by the iterator i in the range [first, last), assigns *i = value and increments value as if by ++value.

Complexity: Exactly last - first increments and assignments.

26.8.13 Greatest common divisor [numeric.ops.gcd]

template <class M, class N> constexpr common_type_t<M,N> gcd(M m, N n);

Requires: |m| shall be representable as a value of type M and |n| shall be representable as a value of type N. [ Note: These requirements ensure, for example, that gcd(m, m) = |m| is representable as a value of type M.  — end note ]

Remarks: If either M or N is not an integer type, or if either is (possibly cv-qualified) bool, the program is ill-formed.

Returns: Zero when m and n are both zero. Otherwise, returns the greatest common divisor of |m| and |n|.

Throws: Nothing.

26.8.14 Least common multiple [numeric.ops.lcm]

template <class M, class N> constexpr common_type_t<M,N> lcm(M m, N n);

Requires: |m| shall be representable as a value of type M and |n| shall be representable as a value of type N. The least common multiple of |m| and |n| shall be representable as a value of type common_type_t<M,N>.

Remarks: If either M or N is not an integer type, or if either is (possibly cv-qualified) bool the program is ill-formed.

Returns: Zero when either m or n is zero. Otherwise, returns the least common multiple of |m| and |n|.

Throws: Nothing.