C++ <numeric> Functions

Beginner
8 min read
Updated: Nov 2026
14 function guides
Function index

About This Index

This page is the C++ <numeric> Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.

Learn what the numeric header is on /cpp/numeric. Use this page when you need a specific function such as accumulate(), partial_sum(), or iota().

Free functions on ranges.

The <numeric> functions are not members of std::vector. You call them as std::accumulate(v.begin(), v.end(), 0) and pass the range as two iterators. They fold a range into one value, write running totals, fill a sequence, or do small maths on two numbers. The 14 functions are listed below with the C++ version each one needs.

How to Call a Numeric Function

Include <numeric>, create a vector, and pass the range to the function:

C++
#include <numeric>
#include <iostream>
#include <vector>

std::vector<int> v(5);

std::iota(v.begin(), v.end(), 1);                      // 1 2 3 4 5
int sum = std::accumulate(v.begin(), v.end(), 0);      // 15
std::vector<int> run(v.size());
std::partial_sum(v.begin(), v.end(), run.begin());     // 1 3 6 10 15
int g = std::gcd(12, 18);                              // 6  (C++17)
int l = std::lcm(4, 6);                                // 12 (C++17)

C++ <numeric> Functions Table

All function guides in this C++ tutorial series (14 tutorials), grouped by purpose.

Reduce / fold

4 functions

Squash a whole range into one value, such as a sum, a product, or a dot product.

FunctionDescriptionSinceTutorial
accumulate()Add up (or fold) a range from left to right, starting from an initial value.C++98Try it
reduce()Combine a range into one value, in any order, so it can run in parallel.C++17Try it
transform_reduce()Transform each element, then reduce the results into one value.C++17Try it
inner_product()Multiply two ranges element by element and add the products (dot product).C++98Try it

Prefix / scan

6 functions

Write a running result for every position: running totals, running products, or differences between neighbours.

FunctionDescriptionSinceTutorial
partial_sum()Write the running (prefix) totals of a range to an output.C++98Try it
inclusive_scan()Write running totals where each output includes its own element (C++17).C++17Try it
exclusive_scan()Write running totals that leave out the current element, starting from init (C++17).C++17Try it
transform_inclusive_scan()Transform each element, then write inclusive running totals (C++17).C++17Try it
transform_exclusive_scan()Transform each element, then write exclusive running totals from init (C++17).C++17Try it
adjacent_difference()Write the difference between each element and the one before it.C++98Try it

Fill / sequence

1 function

Fill a range with a growing sequence of values.

FunctionDescriptionSinceTutorial
iota()Fill a range with a growing sequence: start, start+1, start+2, and so on.C++11Try it

Number theory / helpers

3 functions

Small maths helpers for two numbers: the greatest common divisor, the least common multiple, and the safe midpoint.

FunctionDescriptionSinceTutorial
gcd()Return the greatest common divisor of two integers.C++17Try it
lcm()Return the least common multiple of two integers.C++17Try it
midpoint()Return the point halfway between two values, without overflow.C++20Try it

Frequently Asked Questions

They are free functions from the <numeric> header that work on ranges of numbers: accumulate, reduce, transform_reduce, inner_product, partial_sum, inclusive_scan, exclusive_scan, transform_inclusive_scan, transform_exclusive_scan, adjacent_difference, iota, gcd, lcm, and midpoint.
This series covers 14 functions, grouped as reduce / fold (accumulate, reduce, transform_reduce, inner_product), prefix / scan (partial_sum, inclusive_scan, exclusive_scan, transform_inclusive_scan, transform_exclusive_scan, adjacent_difference), fill / sequence (iota), and number theory / helpers (gcd, lcm, midpoint).
reduce, transform_reduce, inclusive_scan, exclusive_scan, transform_inclusive_scan, transform_exclusive_scan, gcd, and lcm need C++17. midpoint needs C++20. accumulate, inner_product, partial_sum, adjacent_difference, and iota (C++11) work in older standards.
No. They are not member functions. You call them as std::accumulate(v.begin(), v.end(), 0) and pass iterators for the range. The gcd, lcm, and midpoint functions take plain values instead of iterators.
Read the C++ <numeric> page first. Then open accumulate(), partial_sum(), and iota() from the tables below, because they are the easiest to try.

Start with accumulate()

Add up a whole vector in one line, the first thing most programs need from the numeric header.

accumulate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful