Namespaces
Variants

Standard library header <numeric>

From cppreference.com
 
 
Standard library headers
 

This header is part of the numeric library.

Classes

provides a way to store the double-width result of widening arithmetic operations
(class template) [edit]

Functions

(C++11)
fills a range with successive increments of the starting value
(function template & algorithm function object)[edit]
fills a range with successive increments of the starting value
(algorithm function object)[edit]
sums up or folds a range of elements
(function template) [edit]
(C++17)
similar to std::accumulate, except out of order
(function template) [edit]
applies an invocable, then reduces out of order
(function template) [edit]
computes the inner product of two ranges of elements
(function template) [edit]
computes the differences between adjacent elements in a range
(function template) [edit]
computes the partial sum of a range of elements
(function template) [edit]
similar to std::partial_sum, includes the ith input element in the ith sum
(function template) [edit]
similar to std::partial_sum, excludes the ith input element from the ith sum
(function template) [edit]
applies an invocable, then calculates inclusive scan
(function template) [edit]
applies an invocable, then calculates exclusive scan
(function template) [edit]
(C++17)
computes the greatest common divisor of two integers
(function template) [edit]
(C++17)
computes the least common multiple of two integers
(function template) [edit]
(C++20)
midpoint between two numbers or pointers
(function template) [edit]
saturating addition operation on two integers
(function template) [edit]
saturating subtraction operation on two integers
(function template) [edit]
saturating multiplication operation on two integers
(function template) [edit]
saturating division operation on two integers
(function template) [edit]
returns an integer value clamped to the range of another integer type
(function template) [edit]
(C++29)
computes the carry-less product
(function template) [edit]
computes the widening carry-less product
(function template) [edit]

Synopsis

// mostly freestanding
namespace std {
  // accumulate
  template<class InputIter, class T>
  constexpr T accumulate(InputIter first, InputIter last, T init);
  template<class InputIter, class T, class BinaryOperation>
  constexpr T accumulate(InputIter first,
                         InputIter last,
                         T init,
                         BinaryOperation binary_op);

  // reduce
  template<class InputIter>
  constexpr typename iterator_traits<InputIter>::value_type reduce(InputIter first,
                                                                   InputIter last);
  template<class InputIter, class T>
  constexpr T reduce(InputIter first, InputIter last, T init);
  template<class InputIter, class T, class BinaryOperation>
  constexpr T reduce(InputIter first, InputIter last, T init, BinaryOperation binary_op);
  template<class ExecutionPolicy, class ForwardIter>
  typename iterator_traits<ForwardIter>::value_type reduce(
    ExecutionPolicy&& exec,        // freestanding-deleted,
    ForwardIter first,
    ForwardIter last);
  template<class ExecutionPolicy, class ForwardIter, class T>
  T reduce(ExecutionPolicy&& exec, // freestanding-deleted,
           ForwardIter first,
           ForwardIter last,
           T init);
  template<class ExecutionPolicy, class ForwardIter, class T, class BinaryOperation>
  T reduce(ExecutionPolicy&& exec, // freestanding-deleted,
           ForwardIter first,
           ForwardIter last,
           T init,
           BinaryOperation binary_op);

  // inner product
  template<class InputIter1, class InputIter2, class T>
  constexpr T inner_product(InputIter1 first1,
                            InputIter1 last1,
                            InputIter2 first2,
                            T init);
  template<class InputIter1,
           class InputIter2,
           class T,
           class BinaryOperation1,
           class BinaryOperation2>
  constexpr T inner_product(InputIter1 first1,
                            InputIter1 last1,
                            InputIter2 first2,
                            T init,
                            BinaryOperation1 binary_op1,
                            BinaryOperation2 binary_op2);

  // transform reduce
  template<class InputIter1, class InputIter2, class T>
  constexpr T transform_reduce(InputIter1 first1,
                               InputIter1 last1,
                               InputIter2 first2,
                               T init);
  template<class InputIter1,
           class InputIter2,
           class T,
           class BinaryOperation1,
           class BinaryOperation2>
  constexpr T transform_reduce(InputIter1 first1,
                               InputIter1 last1,
                               InputIter2 first2,
                               T init,
                               BinaryOperation1 binary_op1,
                               BinaryOperation2 binary_op2);
  template<class InputIter, class T, class BinaryOperation, class UnaryOperation>
  constexpr T transform_reduce(InputIter first,
                               InputIter last,
                               T init,
                               BinaryOperation binary_op,
                               UnaryOperation unary_op);
  template<class ExecutionPolicy, class ForwardIter1, class ForwardIter2, class T>
  T transform_reduce(ExecutionPolicy&& exec, // freestanding-deleted,
                     ForwardIter1 first1,
                     ForwardIter1 last1,
                     ForwardIter2 first2,
                     T init);
  template<class ExecutionPolicy,
           class ForwardIter1,
           class ForwardIter2,
           class T,
           class BinaryOperation1,
           class BinaryOperation2>
  T transform_reduce(ExecutionPolicy&& exec, // freestanding-deleted,
                     ForwardIter1 first1,
                     ForwardIter1 last1,
                     ForwardIter2 first2,
                     T init,
                     BinaryOperation1 binary_op1,
                     BinaryOperation2 binary_op2);
  template<class ExecutionPolicy,
           class ForwardIter,
           class T,
           class BinaryOperation,
           class UnaryOperation>
  T transform_reduce(ExecutionPolicy&& exec, // freestanding-deleted,
                     ForwardIter first,
                     ForwardIter last,
                     T init,
                     BinaryOperation binary_op,
                     UnaryOperation unary_op);

  // partial sum
  template<class InputIter, class OutputIter>
  constexpr OutputIter partial_sum(InputIter first, InputIter last, OutputIter result);
  template<class InputIter, class OutputIter, class BinaryOperation>
  constexpr OutputIter partial_sum(InputIter first,
                                   InputIter last,
                                   OutputIter result,
                                   BinaryOperation binary_op);

  // exclusive scan
  template<class InputIter, class OutputIter, class T>
  constexpr OutputIter exclusive_scan(InputIter first,
                                      InputIter last,
                                      OutputIter result,
                                      T init);
  template<class InputIter, class OutputIter, class T, class BinaryOperation>
  constexpr OutputIter exclusive_scan(InputIter first,
                                      InputIter last,
                                      OutputIter result,
                                      T init,
                                      BinaryOperation binary_op);
  template<class ExecutionPolicy, class ForwardIter1, class ForwardIter2, class T>
  ForwardIter2 exclusive_scan(ExecutionPolicy&& exec, // freestanding-deleted,
                              ForwardIter1 first,
                              ForwardIter1 last,
                              ForwardIter2 result,
                              T init);
  template<class ExecutionPolicy,
           class ForwardIter1,
           class ForwardIter2,
           class T,
           class BinaryOperation>
  ForwardIter2 exclusive_scan(ExecutionPolicy&& exec, // freestanding-deleted,
                              ForwardIter1 first,
                              ForwardIter1 last,
                              ForwardIter2 result,
                              T init,
                              BinaryOperation binary_op);

  // inclusive scan
  template<class InputIter, class OutputIter>
  constexpr OutputIter inclusive_scan(InputIter first, InputIter last, OutputIter result);
  template<class InputIter, class OutputIter, class BinaryOperation>
  constexpr OutputIter inclusive_scan(InputIter first,
                                      InputIter last,
                                      OutputIter result,
                                      BinaryOperation binary_op);
  template<class InputIter, class OutputIter, class BinaryOperation, class T>
  constexpr OutputIter inclusive_scan(InputIter first,
                                      InputIter last,
                                      OutputIter result,
                                      BinaryOperation binary_op,
                                      T init);
  template<class ExecutionPolicy, class ForwardIter1, class ForwardIter2>
  ForwardIter2 inclusive_scan(ExecutionPolicy&& exec, // freestanding-deleted,
                              ForwardIter1 first,
                              ForwardIter1 last,
                              ForwardIter2 result);
  template<class ExecutionPolicy,
           class ForwardIter1,
           class ForwardIter2,
           class BinaryOperation>
  ForwardIter2 inclusive_scan(ExecutionPolicy&& exec, // freestanding-deleted,
                              ForwardIter1 first,
                              ForwardIter1 last,
                              ForwardIter2 result,
                              BinaryOperation binary_op);
  template<class ExecutionPolicy,
           class ForwardIter1,
           class ForwardIter2,
           class BinaryOperation,
           class T>
  ForwardIter2 inclusive_scan(ExecutionPolicy&& exec, // freestanding-deleted,
                              ForwardIter1 first,
                              ForwardIter1 last,
                              ForwardIter2 result,
                              BinaryOperation binary_op,
                              T init);

  // transform exclusive scan
  template<class InputIter,
           class OutputIter,
           class T,
           class BinaryOperation,
           class UnaryOperation>
  constexpr OutputIter transform_exclusive_scan(InputIter first,
                                                InputIter last,
                                                OutputIter result,
                                                T init,
                                                BinaryOperation binary_op,
                                                UnaryOperation unary_op);
  template<class ExecutionPolicy,
           class ForwardIter1,
           class ForwardIter2,
           class T,
           class BinaryOperation,
           class UnaryOperation>
  ForwardIter2 transform_exclusive_scan(ExecutionPolicy&& exec, // freestanding-deleted,
                                        ForwardIter1 first,
                                        ForwardIter1 last,
                                        ForwardIter2 result,
                                        T init,
                                        BinaryOperation binary_op,
                                        UnaryOperation unary_op);

  // transform inclusive scan
  template<class InputIter, class OutputIter, class BinaryOperation, class UnaryOperation>
  constexpr OutputIter transform_inclusive_scan(InputIter first,
                                                InputIter last,
                                                OutputIter result,
                                                BinaryOperation binary_op,
                                                UnaryOperation unary_op);
  template<class InputIter,
           class OutputIter,
           class BinaryOperation,
           class UnaryOperation,
           class T>
  constexpr OutputIter transform_inclusive_scan(InputIter first,
                                                InputIter last,
                                                OutputIter result,
                                                BinaryOperation binary_op,
                                                UnaryOperation unary_op,
                                                T init);
  template<class ExecutionPolicy,
           class ForwardIter1,
           class ForwardIter2,
           class BinaryOperation,
           class UnaryOperation>
  ForwardIter2 transform_inclusive_scan(ExecutionPolicy&& exec, // freestanding-deleted,
                                        ForwardIter1 first,
                                        ForwardIter1 last,
                                        ForwardIter2 result,
                                        BinaryOperation binary_op,
                                        UnaryOperation unary_op);
  template<class ExecutionPolicy,
           class ForwardIter1,
           class ForwardIter2,
           class BinaryOperation,
           class UnaryOperation,
           class T>
  ForwardIter2 transform_inclusive_scan(ExecutionPolicy&& exec, // freestanding-deleted,
                                        ForwardIter1 first,
                                        ForwardIter1 last,
                                        ForwardIter2 result,
                                        BinaryOperation binary_op,
                                        UnaryOperation unary_op,
                                        T init);

  // adjacent difference
  template<class InputIter, class OutputIter>
  constexpr OutputIter adjacent_difference(InputIter first,
                                           InputIter last,
                                           OutputIter result);
  template<class InputIter, class OutputIter, class BinaryOperation>
  constexpr OutputIter adjacent_difference(InputIter first,
                                           InputIter last,
                                           OutputIter result,
                                           BinaryOperation binary_op);
  template<class ExecutionPolicy, class ForwardIter1, class ForwardIter2>
  ForwardIter2 adjacent_difference(ExecutionPolicy&& exec, // freestanding-deleted,
                                   ForwardIter1 first,
                                   ForwardIter1 last,
                                   ForwardIter2 result);
  template<class ExecutionPolicy,
           class ForwardIter1,
           class ForwardIter2,
           class BinaryOperation>
  ForwardIter2 adjacent_difference(ExecutionPolicy&& exec, // freestanding-deleted,
                                   ForwardIter1 first,
                                   ForwardIter1 last,
                                   ForwardIter2 result,
                                   BinaryOperation binary_op);

  // iota
  template<class ForwardIter, class T>
  constexpr void iota(ForwardIter first, ForwardIter last, T value);

  namespace ranges {
    template<class O, class T> using iota_result = out_value_result<O, T>;

    template<input_or_output_iterator O, sentinel_for<O> S, weakly_incrementable T>
      requires indirectly_writable<O, const T&>
    constexpr iota_result<O, T> iota(O first, S last, T value);

    template<weakly_incrementable T, output_range<const T&> R>
    constexpr iota_result<borrowed_iterator_t<R>, T> iota(R&& r, T value);
  }

  // greatest common divisor
  template<class M, class N> constexpr common_type_t<M, N> gcd(M m, N n);

  // least common multiple
  template<class M, class N> constexpr common_type_t<M, N> lcm(M m, N n);

  // midpoint
  template<class T> constexpr T midpoint(T a, T b) noexcept;
  template<class T> constexpr T* midpoint(T* a, T* b);

  // saturation arithmetic
  template<class T> constexpr T saturating_add(T x, T y) noexcept;
  template<class T> constexpr T saturating_sub(T x, T y) noexcept;
  template<class T> constexpr T saturating_mul(T x, T y) noexcept;
  template<class T> constexpr T saturating_div(T x, T y) noexcept;
  template<class T, class U> constexpr T saturating_cast(U x) noexcept;

  // carry-less product
  template<class T>
    class wide_result;
  
  template<class T>
    constexpr T clmul(T x, T y) noexcept;
  template<class T>
    constexpr wide_result<T> widening_clmul(T x, T y) noexcept;  
}

Class template std::wide_result

namespace std {
  template<class T>
    class wide_result {
    public:
      constexpr wide_result(T high, T low) noexcept
        : /*high-bits*/(high), /*low-bits*/(low) {}
    
      constexpr T high() const noexcept { return /*high-bits*/; }
      constexpr T low() const noexcept { return /*low-bits*/; }
    
      friend constexpr bool operator==(const wide_result&, const wide_result&) = default;
      friend constexpr strong_ordering operator<=>(const wide_result& x,
                                                   const wide_result& y) noexcept;
    private:
      T /*high-bits*/, /*low-bits*/; // exposition-only
  };
}