In C++17, std::exclusive_scan() writes running totals that leave out the current element. Output i is the sum of all inputs before position i, and the very first output is the start value init.
For the input 1 2 3 4 and init = 0 the output is 0 1 3 6. This is perfect for start offsets: if each item has a length, the scan tells you where each item begins.
This function is declared in the <numeric> header. It needs C++17 (-std=c++17). It is a free function, called as std::exclusive_scan(...), not with a dot.
Syntax
Give the input range, where to write, and a start value. A custom operation is optional.
C++
auto e1 = std::exclusive_scan(first, last, d_first, init);
auto e2 = std::exclusive_scan(first, last, d_first, init, op);
Call
What it does
exclusive_scan(first, last, d_first, init)
Writes init, then init + a0, then init + a0 + a1, and so on. The last input is not added to any output.
exclusive_scan(first, last, d_first, init, op)
Same, but uses op instead of +. op must be associative.
Unlike inclusive_scan(), init is required and comes before the operation. For a product, start with 1, not 0.
Library declarations
C++
template<class InputIt, class OutputIt, class T>
OutputIt exclusive_scan(InputIt first, InputIt last,
OutputIt d_first, T init);
template<class InputIt, class OutputIt, class T, class BinaryOp>
OutputIt exclusive_scan(InputIt first, InputIt last,
OutputIt d_first, T init, BinaryOp op);
// 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 needs room for as many elements as the input.
init
Required. The value of the first output. It is also combined into every later output.
op
Optional. An associative binary operation. The default is +.
Return Value
Return Value
Description
Output iterator
An iterator one past the last element written to the output.
C++ Examples
Example 1 — Running Sum That Leaves Out the Current Element
With init = 0, the first output is 0 and every other output is the sum of the elements before it.
C++
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> out(v.size());
std::exclusive_scan(v.begin(), v.end(), out.begin(), 0);
for (int n : out) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
0 1 3 6 10
How It Works
The fourth argument, 0, is init. It becomes the very first output, because nothing comes before the first element.
The second output is 0+1 = 1. It holds only the first element, not the second. The third output is 1+2 = 3.
The fifth input, 5, is never added to any output. The same input with inclusive_scan() would end with 15.
Example 2 — Exclusive Product with init = 1
For a product, the start value must be 1. Pass std::multiplies as the operation.
C++
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {2, 3, 4, 5};
std::vector<int> out(v.size());
std::exclusive_scan(v.begin(), v.end(), out.begin(), 1,
std::multiplies<>());
for (int n : out) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 2 6 24
How It Works
The start value is 1, the identity for multiplication. A start of 0 would make every output 0.
The first output is 1. The second is 1*2 = 2, and the third is 2*3 = 6.
The fourth output is 6*4 = 24. The last input, 5, is left out, so 120 never shows up.
Example 3 — Find Start Positions of Items
If every item has a length, an exclusive scan of the lengths gives the position where each item starts.
C++
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> lengths = {3, 2, 5, 1};
std::vector<int> starts(lengths.size());
std::exclusive_scan(lengths.begin(), lengths.end(),
starts.begin(), 0);
for (std::size_t i = 0; i < starts.size(); ++i) {
std::cout << "item " << i << " starts at " << starts[i] << '\n';
}
int total = starts.back() + lengths.back();
std::cout << "total length: " << total << '\n';
return 0;
}
Output
item 0 starts at 0
item 1 starts at 3
item 2 starts at 5
item 3 starts at 10
total length: 11
How It Works
There are four items with lengths 3, 2, 5 and 1. Think of them laid out one after another in a buffer.
The first item starts at 0. The second starts after the first item, at 3. The third starts at 3+2 = 5, and the fourth at 5+5 = 10.
Because the scan leaves out the last length, the total is the last start plus the last length: 10 + 1 = 11.
Frequently Asked Questions
It writes prefix totals that leave out the current element. Output i is the start value combined with all inputs before position i. For 1 2 3 4 and init 0, the output is 0 1 3 6.
inclusive_scan() includes the current element in each output, so 1 2 3 4 gives 1 3 6 10. exclusive_scan() leaves it out and starts with init, so the same input gives 0 1 3 6.
Use it to compute offsets or positions, for example where each word starts in a buffer. The first item starts at init, usually 0.
The first output has no element before it, so it needs a value. It should be the identity of your operation: 0 for plus, 1 for multiply.
No. Before C++17 use partial_sum() and shift the result by one place, or write a loop.