C++ std::inclusive_scan() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++17

Definition

In C++17, std::inclusive_scan() is the modern version of std::partial_sum(). It writes the prefix totals of a range, where each output includes the element at the same position.

For the input 1 2 3 4 the output is 1 3 6 10. Unlike partial_sum(), it may regroup the work, so it can run in parallel with an execution policy. It also has a form with an initial value.

This function is declared in the <numeric> header. It needs C++17 (-std=c++17). It is a free function, called as std::inclusive_scan(...), not with a dot.

Syntax

Give the input range and where to write. An operation and an initial value are optional.

C++
auto e1 = std::inclusive_scan(first, last, d_first);
auto e2 = std::inclusive_scan(first, last, d_first, op);
auto e3 = std::inclusive_scan(first, last, d_first, op, init);
CallWhat it does
inclusive_scan(first, last, d_first)Writes running sums. Output i is the sum of inputs 0 to i.
inclusive_scan(first, last, d_first, op)Same, but combines values with op instead of +. op must be associative.
inclusive_scan(first, last, d_first, op, init)Same, but init is combined into every output as if it came before the first element.

The only difference from exclusive_scan() is whether the current element is part of its own output. Here it is.

Library declarations
C++
template<class InputIt, class OutputIt>
OutputIt inclusive_scan(InputIt first, InputIt last, OutputIt d_first);

template<class InputIt, class OutputIt, class BinaryOp>
OutputIt inclusive_scan(InputIt first, InputIt last,
                        OutputIt d_first, BinaryOp op);

template<class InputIt, class OutputIt, class BinaryOp, class T>
OutputIt inclusive_scan(InputIt first, InputIt last,
                        OutputIt d_first, BinaryOp op, T init);

// Every form also exists with an ExecutionPolicy&& argument.

Parameters

ParameterDescription
first, lastRequired. The input range [first, last).
d_firstRequired. The start of the output. It must have room for the same number of elements.
opOptional. An associative binary operation. The default is +.
initOptional. A start value that is combined into every output. It comes last in the argument list, after op.

Return Value

Return ValueDescription
Output iteratorAn iterator one past the last element written to the output.

C++ Examples

Example 1 — Running Sum with inclusive_scan()

The simplest call: three arguments and the default plus.

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

int main() {
    std::vector<int> v = {1, 2, 3, 4};
    std::vector<int> out(v.size());

    std::inclusive_scan(v.begin(), v.end(), out.begin());

    for (int n : out) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. out is created with the same size as v, so it has a slot for every output.
  2. The first output includes only the first element, 1. The second output includes the first two, 1+2 = 3.
  3. The third and fourth outputs are 6 and 10. The last one equals the total of the whole input.

Example 2 — Running Maximum with a Lambda

Replace plus with a function that keeps the larger value to see the best result so far.

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

int main() {
    std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6};
    std::vector<int> out(v.size());

    std::inclusive_scan(v.begin(), v.end(), out.begin(),
                        [](int a, int b) { return std::max(a, b); });

    for (int n : out) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The lambda returns the bigger of two numbers. Taking a maximum is associative, so it is a valid operation for a scan.
  2. Each output is the biggest value seen up to that point. The max grows to 3, 4, 5 and finally 9.
  3. After 9 nothing bigger comes, so the outputs stay at 9 for the rest of the line. A lambda is used because std::max has several overloads.

Example 3 — inclusive_scan() with init

The initial value is added to every output, as if it were an element before the first one.

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

int main() {
    std::vector<int> v = {1, 2, 3, 4};
    std::vector<int> out(v.size());

    std::inclusive_scan(v.begin(), v.end(), out.begin(),
                        std::plus<>(), 100);

    for (int n : out) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. This form needs the operation before the start value, so we pass std::plus<>() first and then 100.
  2. The scan behaves as if the sequence were 100 1 2 3 4, but 100 itself is not written to the output.
  3. The outputs are 100+1 = 101, 101+2 = 103, 103+3 = 106, and 106+4 = 110.

Frequently Asked Questions

It writes the prefix totals of a range, where each output includes the element at the same position. For 1 2 3 4 it writes 1 3 6 10. It was added in C++17.
In inclusive_scan() output i includes input i. In exclusive_scan() it includes only the elements before input i. For 1 2 3 4 you get 1 3 6 10 and 0 1 3 6.
They give the same result for addition. inclusive_scan() may regroup the work, supports execution policies, and has an init overload. partial_sum() is strictly sequential.
Because inclusive_scan() may split the range and combine the pieces in a different grouping. For + and max, any grouping gives the same answer. For subtraction the answer would change.
It is the last argument, after the operation: inclusive_scan(first, last, d_first, op, init). You cannot pass init without op.

Next: inner_product()

Multiply two ranges element by element and add the products (dot product) with <numeric>.

inner_product() 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.

11 people found this page helpful