C++ std::transform_exclusive_scan() Function

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

Definition

In C++17, std::transform_exclusive_scan() first transforms each element with a function, and then writes the exclusive running totals of the transformed values, starting from init.

The word exclusive means each output leaves out the value at its own position. For the input 1 2 3 4, a squaring function, and init = 0, the transformed values are 1 4 9 16 and the output is 0 1 5 14. Include <numeric> and use C++17.

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

Syntax

Give the input, the output, a start value, the combining operation, and the transform function. All of them are required.

C++
auto e = std::transform_exclusive_scan(first, last, d_first,
                                       init, op, top);
CallWhat it does
transform_exclusive_scan(first, last, d_first, init, op, top)Applies top to each element, then writes the exclusive running result of op, starting with init.

The order is init, then the combining operation, then the transform. The inclusive version has a different order.

Library declarations
C++
template<class InputIt, class OutputIt, class T,
         class BinaryOp, class UnaryOp>
OutputIt transform_exclusive_scan(InputIt first, InputIt last,
                                  OutputIt d_first, T init,
                                  BinaryOp op, UnaryOp top);

// Also available with a leading ExecutionPolicy&& argument.

Parameters

ParameterDescription
first, lastRequired. The input range [first, last).
d_firstRequired. The start of the output. It needs room for as many elements as the input.
initRequired. The value of the first output. For addition use 0, and for multiplication use 1.
opRequired. An associative binary operation that combines the transformed values.
topRequired. A unary function applied to every element before it is combined.

Return Value

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

C++ Examples

Example 1 — Exclusive Running Sum of Squares

Square each element, but write only the total of the squares that come before it.

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_exclusive_scan(
        v.begin(), v.end(), out.begin(),
        0,
        std::plus<>(),
        [](int x) { return x * x; });

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

How It Works

  1. The transform makes the values 1 4 9 16. The start value 0 comes right after the output iterator.
  2. The first output is 0, because nothing comes before the first element. The second output is 0+1 = 1, and the third is 1+4 = 5.
  3. The fourth output is 5+9 = 14. The last square, 16, is left out. This is what makes the scan exclusive.

Example 2 — Doubled Values with a Start of 100

Double every number, then keep an exclusive running total that starts from 100.

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

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

    std::transform_exclusive_scan(
        v.begin(), v.end(), out.begin(),
        100,
        std::plus<>(),
        [](int x) { return x * 2; });

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

How It Works

  1. The lambda doubles each number, so the transformed values are 2 4 6.
  2. The first output is the start value 100. Then 100+2 = 102, and 102+4 = 106.
  3. The last doubled value, 6, is not used. If you want the grand total 112, add it to the last output yourself.

Example 3 — Count Odd Numbers Before Each Position

Turn each number into 1 (odd) or 0 (even), then count how many odd numbers come before each position.

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

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

    std::transform_exclusive_scan(
        v.begin(), v.end(), out.begin(),
        0,
        std::plus<>(),
        [](int x) { return x % 2; });

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

How It Works

  1. The lambda x % 2 gives 1 for an odd number and 0 for an even number. The flags are 1 0 1 0 1.
  2. Each output counts the odd numbers strictly before its position. Before the first number there are none, so the output starts with 0.
  3. Position 3 holds 3 and has one odd number before it, so its output is 1. The last output is 2, because only 1 and 3 come before the last element.

Frequently Asked Questions

It applies a unary function to every element and then writes the exclusive prefix totals of the results, starting from init. It is transform() plus exclusive_scan() in one pass.
The call is transform_exclusive_scan(first, last, d_first, init, op, top). The start value comes first, then the binary combine operation, then the unary transform function.
The scan is exclusive: output i only contains values from positions before i. The last value would belong to an output after the end.
The inclusive version includes the transformed value of the current position and has an optional init. The exclusive version leaves it out and always needs init.
C++17. It is declared in <numeric> and also has overloads that take an execution policy.

Next: transform_inclusive_scan()

Transform each element, then write inclusive running totals (C++17) with <numeric>.

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

9 people found this page helpful