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);
Call
What 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
Parameter
Description
first, last
Required. The input range [first, last).
d_first
Required. The start of the output. It must have room for the same number of elements.
op
Optional. An associative binary operation. The default is +.
init
Optional. A start value that is combined into every output. It comes last in the argument list, after op.
Return Value
Return Value
Description
Output iterator
An 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;
}
Output
1 3 6 10
How It Works
out is created with the same size as v, so it has a slot for every output.
The first output includes only the first element, 1. The second output includes the first two, 1+2 = 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;
}
Output
3 3 4 4 5 9 9 9
How It Works
The lambda returns the bigger of two numbers. Taking a maximum is associative, so it is a valid operation for a scan.
Each output is the biggest value seen up to that point. The max grows to 3, 4, 5 and finally 9.
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;
}
Output
101 103 106 110
How It Works
This form needs the operation before the start value, so we pass std::plus<>() first and then 100.
The scan behaves as if the sequence were 100 1 2 3 4, but 100 itself is not written to the output.
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.