In C++, <numeric> is a standard header with small algorithms for numbers. Most of its functions take a range (two iterators such as v.begin() and v.end()) and either fold it into one value or write a new sequence of values.
The most common ones are accumulate() to add up a vector, partial_sum() for running totals, and iota() to fill a vector with 1 2 3 .... The header also has gcd(), lcm(), and midpoint() for two numbers. Write #include <numeric> to use them.
This page teaches what the header is for. The Numeric Functions index lists every function guide in this series.
Free functions, not members.
These are not called with a dot like v.push_back(). You write std::accumulate(v.begin(), v.end(), 0) and give the range as arguments. Some of them are newer: reduce(), the scans, gcd() and lcm() need C++17, and midpoint() needs C++20.
Syntax
C++ Syntax
Common ways to use the <numeric> functions on a std::vector:
C++
#include <iostream>
#include <numeric>
#include <vector>
std::vector<int> v(5);
std::vector<int> out(5);
std::iota(v.begin(), v.end(), 1); // 1 2 3 4 5
int sum = std::accumulate(v.begin(), v.end(), 0); // 15
int sum2 = std::reduce(v.begin(), v.end()); // 15 (C++17)
int dot = std::inner_product(v.begin(), v.end(), v.begin(), 0); // 55
std::partial_sum(v.begin(), v.end(), out.begin()); // 1 3 6 10 15
std::exclusive_scan(v.begin(), v.end(), out.begin(), 0); // 0 1 3 6 10 (C++17)
int g = std::gcd(12, 18); // 6 (C++17)
int m = std::midpoint(10, 20); // 15 (C++20)
Reference
accumulate vs reduce vs transform_reduce
These three fold a range into one value, but they are not the same. Here is a quick comparison:
Both calls add 10 + 20 + 30 to 0, and both give 60. Addition of integers gives the same answer in any order.
reduce() is allowed to add the numbers in any order or on several threads. That is fine for +, but it is why it needs a C++17 compiler and an operation that does not depend on order.
The subtraction runs strictly left to right: 100-10 = 90, 90-20 = 70, 70-30 = 40. Do not give a subtraction lambda to reduce(), because its answer would not be fixed.
Example 3 — Scans Instead of a Hand-Written Loop
A loop that builds running totals is easy to get wrong. inclusive_scan() and exclusive_scan() do the same job in one line.
C++
#include <iostream>
#include <numeric>
#include <vector>
void show(const char* name, const std::vector<int>& v) {
std::cout << name;
for (int n : v) std::cout << ' ' << n;
std::cout << '\n';
}
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
std::vector<int> byLoop;
int total = 0;
for (int n : v) {
total += n;
byLoop.push_back(total);
}
std::vector<int> inc(v.size());
std::vector<int> exc(v.size());
std::inclusive_scan(v.begin(), v.end(), inc.begin());
std::exclusive_scan(v.begin(), v.end(), exc.begin(), 0);
show("loop: ", byLoop);
show("inclusive: ", inc);
show("exclusive: ", exc);
return 0;
}
The hand-written loop keeps a variable total, adds each number, and pushes the new total. It needs three lines and one extra variable.
inclusive_scan() gives exactly the same answer in one call, because each output includes its own element: 3 4 8 9 14.
exclusive_scan() with init = 0 leaves out the current element, so every value is shifted one place to the right and starts with 0: 0 3 4 8 9. This shape is handy for start positions.
Continue
Next: C++ <numeric> Functions Index
After you know what the header is for, open the function index for a full tutorial on each function.
<numeric> is a standard header with small algorithms for numbers. It has functions to sum a range (accumulate, reduce), multiply and add two ranges (inner_product, transform_reduce), write running totals (partial_sum and the scan functions), fill a sequence (iota), and do simple maths (gcd, lcm, midpoint).
accumulate() always goes from left to right, so any operation works, even subtraction. reduce() (C++17) may combine the elements in any order and may run in parallel, so the operation must be associative and commutative, like + on integers.
Use partial_sum(), inclusive_scan(), or exclusive_scan() when you need running totals. They are shorter than a loop and harder to get wrong. inclusive_scan() and exclusive_scan() can also run in parallel on big data.
reduce, transform_reduce, inclusive_scan, exclusive_scan, transform_inclusive_scan, transform_exclusive_scan, gcd and lcm need C++17. midpoint needs C++20. The others work in older standards.
Open the C++ <numeric> Functions index. It lists all 14 functions with a short description, the C++ version needed, and a full tutorial for each.