In C++17, std::transform_inclusive_scan() combines two jobs: it first transforms every element with a function, and then writes the inclusive running totals of the transformed values.
It saves you from making a temporary vector. For the input 1 2 3 4 and a squaring function, the transformed values are 1 4 9 16 and the output is 1 5 14 30. Each output includes its own element.
This function is declared in the <numeric> header. It needs C++17 (-std=c++17). It is a free function, called as std::transform_inclusive_scan(...), not with a dot.
Syntax
Give the input, the output, the combining operation, and the transform function. An initial value is optional.
C++
auto e1 = std::transform_inclusive_scan(first, last, d_first, op, top);
auto e2 = std::transform_inclusive_scan(first, last, d_first, op, top, init);
Call
What it does
transform_inclusive_scan(first, last, d_first, op, top)
Applies top to each element, then writes the inclusive running result of op.
transform_inclusive_scan(first, last, d_first, op, top, init)
Same, but init is combined into every output as if it came first.
Watch the argument order: the combining operation op comes first, the transformtop second.
Library declarations
C++
template<class InputIt, class OutputIt, class BinaryOp, class UnaryOp>
OutputIt transform_inclusive_scan(InputIt first, InputIt last,
OutputIt d_first,
BinaryOp op, UnaryOp top);
template<class InputIt, class OutputIt, class BinaryOp, class UnaryOp, class T>
OutputIt transform_inclusive_scan(InputIt first, InputIt last,
OutputIt d_first,
BinaryOp op, UnaryOp top, 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 needs room for the same number of elements as the input.
op
Required. An associative binary operation that combines the transformed values, such as std::plus<>().
top
Required. A unary function applied to every input element before it is combined.
init
Optional. A start value combined into every output.
Return Value
Return Value
Description
Output iterator
An iterator one past the last element written to the output.
C++ Examples
Example 1 — Running Sum of Squares
Square each number and write the running total of the squares.
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::transform_inclusive_scan(
v.begin(), v.end(), out.begin(),
std::plus<>(),
[](int x) { return x * x; });
for (int n : out) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 5 14 30
How It Works
The lambda is the transform step. It changes 1 2 3 4 into 1 4 9 16 without storing them anywhere.
std::plus<>() is the combine step. The running totals of the squares are 1, 1+4 = 5, and 5+9 = 14.
The last output is 14+16 = 30. The original vector v still holds 1 2 3 4.
Example 2 — Running Sum of Absolute Values with init
Turn negative numbers positive, then keep a running total that starts from 10.
The transform step uses std::abs(), so the values become 3 1 4 2. The minus signs are gone.
The last argument 10 is init. It is combined into every output, but it is not written as an output of its own.
The outputs are 10+3 = 13, 13+1 = 14, 14+4 = 18, and 18+2 = 20.
Example 3 — Count Even Numbers So Far
Turn each number into 1 (even) or 0 (odd), then keep a running count.
C++
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 6};
std::vector<int> out(v.size());
std::transform_inclusive_scan(
v.begin(), v.end(), out.begin(),
std::plus<>(),
[](int x) { return x % 2 == 0 ? 1 : 0; });
for (int n : out) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
0 1 2 2 3
How It Works
The transform lambda gives 1 for an even number and 0 for an odd number. The input 1 2 4 5 6 becomes 0 1 1 0 1.
The running total of those flags is the count of even numbers seen so far.
The outputs are 0, 1, 2, 2, 3. The count stays at 2 when the odd number 5 is passed, and the last output says three even numbers exist.
Frequently Asked Questions
It applies a unary function to every element and then writes the inclusive prefix totals of the results. It is transform() plus inclusive_scan() in one pass, without a temporary vector.
First the binary operation (for example std::plus<>()), then the unary transform function: transform_inclusive_scan(first, last, d_first, op, top).
No. The transform is applied to a copy of each value during the scan. The input range stays the same.
In transform_inclusive_scan() each output includes the transformed value of its own position. In transform_exclusive_scan() it leaves that value out and starts with init.
C++17. The function lives in the <numeric> header, and it also has overloads with execution policies for parallel work.