C++ <numeric> Header

Beginner
8 min read
Updated: Nov 2026
3 examples
<numeric>

Definition and Usage

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.

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)

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:

Featureaccumulatereducetransform_reduce
C++ versionC++98C++17C++17
Order of workAlways left to rightAny orderAny order
Can run in parallelNoYes, with an execution policyYes, with an execution policy
Operation must beAnythingAssociative and commutativeAssociative and commutative
Changes each element firstOnly inside your own lambdaNoYes, with a transform function
Typical useSum, product, join strings, subtractionSum or product of a large vectorSum of squares, dot product

Which Numeric Function Should I Use?

Pick the function by the job you want to do:

FunctionUse it when you want to…
accumulate() / reduce()Add up or multiply a whole range. Use accumulate() when order matters and reduce() for big data.
inner_product() / transform_reduce()Combine two ranges position by position, for example a dot product or a weighted total
partial_sum() / inclusive_scan()Write running totals where each output includes its own element
exclusive_scan()Write running totals that leave out the current element, such as start positions
adjacent_difference()Find the gap between each element and the one before it
iota()Fill a vector with 1 2 3 ... or an index array 0 1 2 ...
gcd() / lcm() / midpoint()Do number maths on two values: common divisor, common multiple, or a safe halfway point

C++ Examples

Three short programs that show how the <numeric> functions work.

Example 1 — Fill, Sum and Running Totals with iota(), accumulate() and partial_sum()

A first program that makes the numbers 1 to 5, adds them up, and writes the running totals.

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

int main() {
    std::vector<int> v(5);
    std::iota(v.begin(), v.end(), 1);

    int sum = std::accumulate(v.begin(), v.end(), 0);

    std::vector<int> run(v.size());
    std::partial_sum(v.begin(), v.end(), run.begin());

    std::cout << "values:";
    for (int n : v) std::cout << ' ' << n;
    std::cout << '\n';
    std::cout << "sum: " << sum << '\n';
    std::cout << "running totals:";
    for (int n : run) std::cout << ' ' << n;
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::vector<int> v(5) makes five empty slots, and iota() fills them with 1 2 3 4 5 without a loop.
  2. accumulate() starts from 0 and adds each element in turn, so sum becomes 15.
  3. partial_sum() writes the total so far into run at each step: 1, 1+2, 3+3, 6+4, 10+5. The last running total is the same as the sum.

Example 2 — Choose Between accumulate() and reduce()

Both can add up numbers. Only accumulate() promises a left-to-right order, so only it is safe for subtraction.

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

int main() {
    std::vector<int> v = {10, 20, 30};

    int a = std::accumulate(v.begin(), v.end(), 0);
    int r = std::reduce(v.begin(), v.end(), 0);

    // Subtraction depends on order: only accumulate() is safe.
    int minus = std::accumulate(v.begin(), v.end(), 100,
                                [](int acc, int x) { return acc - x; });

    std::cout << "accumulate sum: " << a << '\n';
    std::cout << "reduce sum: " << r << '\n';
    std::cout << "accumulate minus: " << minus << '\n';
    return 0;
}

How It Works

  1. Both calls add 10 + 20 + 30 to 0, and both give 60. Addition of integers gives the same answer in any order.
  2. 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.
  3. 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;
}

How It Works

  1. The hand-written loop keeps a variable total, adds each number, and pushes the new total. It needs three lines and one extra variable.
  2. inclusive_scan() gives exactly the same answer in one call, because each output includes its own element: 3 4 8 9 14.
  3. 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.

Next: C++ <numeric> Functions Index

After you know what the header is for, open the function index for a full tutorial on each function.

Start withWhy
accumulate()Add up a vector, the most common numeric job
iota()Fill a vector with a growing sequence
partial_sum()Write running totals into another vector
gcd()Find the greatest common divisor of two numbers

Full list: C++ <numeric> Functions index (/cpp/numeric/function).

Frequently Asked Questions

<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.

Browse every numeric function

Open the functions index for categorized guides with examples and FAQs.

Numeric Functions Index →

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.

8 people found this page helpful