C++ <numeric> Functions

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().
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:
#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 functionsSquash a whole range into one value, such as a sum, a product, or a dot product.
| Function | Description | Since | Tutorial |
|---|---|---|---|
accumulate() | Add up (or fold) a range from left to right, starting from an initial value. | C++98 | Try it |
reduce() | Combine a range into one value, in any order, so it can run in parallel. | C++17 | Try it |
transform_reduce() | Transform each element, then reduce the results into one value. | C++17 | Try it |
inner_product() | Multiply two ranges element by element and add the products (dot product). | C++98 | Try it |
Prefix / scan
6 functionsWrite a running result for every position: running totals, running products, or differences between neighbours.
| Function | Description | Since | Tutorial |
|---|---|---|---|
partial_sum() | Write the running (prefix) totals of a range to an output. | C++98 | Try it |
inclusive_scan() | Write running totals where each output includes its own element (C++17). | C++17 | Try it |
exclusive_scan() | Write running totals that leave out the current element, starting from init (C++17). | C++17 | Try it |
transform_inclusive_scan() | Transform each element, then write inclusive running totals (C++17). | C++17 | Try it |
transform_exclusive_scan() | Transform each element, then write exclusive running totals from init (C++17). | C++17 | Try it |
adjacent_difference() | Write the difference between each element and the one before it. | C++98 | Try it |
Fill / sequence
1 functionFill a range with a growing sequence of values.
Number theory / helpers
3 functionsSmall maths helpers for two numbers: the greatest common divisor, the least common multiple, and the safe midpoint.
Frequently Asked Questions
Start with accumulate()
Add up a whole vector in one line, the first thing most programs need from the numeric header.
12 people found this page helpful
