C++ std::transform_reduce() Function

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

Definition

In C++17, std::transform_reduce() does a map and then reduce in a single call. First it changes every element with a transform function, then it combines all the changed values into one result.

With one range it can compute things like the sum of squares. With two ranges it compares or multiplies elements side by side, so it can compute a dot product. Like std::reduce(), it may reorder the work and run in parallel.

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

Syntax

There is a form for one range and a form for two ranges. Both take a start value.

C++
// One range
T a = std::transform_reduce(first, last, init, reduce_op, transform_op);

// Two ranges (dot product by default)
T b = std::transform_reduce(first1, last1, first2, init);
T c = std::transform_reduce(first1, last1, first2, init, reduce_op, transform_op);
CallWhat it does
transform_reduce(first, last, init, rop, top)Applies top to each element, then combines the results with rop, starting from init.
transform_reduce(f1, l1, f2, init)Multiplies matching elements of two ranges and adds them up. This is the dot product.
transform_reduce(f1, l1, f2, init, rop, top)Applies top(a, b) to each pair of matching elements, then combines the results with rop.

Two steps in one: top is the map step and rop is the reduce step. The reduce operation must be associative and commutative.

Library declarations
C++
template<class InputIt, class T, class BinaryReduce, class UnaryTransform>
T transform_reduce(InputIt first, InputIt last, T init,
                   BinaryReduce reduce, UnaryTransform transform);

template<class InputIt1, class InputIt2, class T>
T transform_reduce(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init);

// Also: the two-range form with reduce and transform operations,
// and every form with an ExecutionPolicy&&.

Parameters

ParameterDescription
first, lastRequired. The range [first, last). In the two-range form they are first1 and last1.
first2Two-range form only. The start of the second range, which must have at least as many elements as the first.
initRequired. The start value of the result. Its type is the type of the result.
reduce_opCombines two values into one. It must be associative and commutative. The default is +.
transform_opChanges one element, or one pair of elements. The default for two ranges is *.

Return Value

Return ValueDescription
Value of type TThe reduced value. For an empty range it is init.

C++ Examples

Example 1 — Sum of Squares in One Pass

Square every number and add the squares, without making a second vector.

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

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

    int total = std::transform_reduce(
        v.begin(), v.end(), 0,
        std::plus<>(),
        [](int x) { return x * x; });

    std::cout << "sum of squares = " << total << '\n';
    return 0;
}

How It Works

  1. The transform step is the lambda [](int x) { return x * x; }. It turns 1 2 3 4 into 1 4 9 16 on the fly.
  2. The reduce step is std::plus<>(). It adds the squared values to init, which is 0.
  3. 1 + 4 + 9 + 16 is 30. No temporary vector was needed.

Example 2 — Dot Product of Two Vectors

With two ranges and no operations, transform_reduce() multiplies pairs and adds them.

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

int main() {
    std::vector<int> a = {1, 2, 3};
    std::vector<int> b = {4, 5, 6};

    int dot = std::transform_reduce(a.begin(), a.end(), b.begin(), 0);

    std::cout << "dot product = " << dot << '\n';
    return 0;
}

How It Works

  1. There are two vectors of the same size. The second one is given only by its start iterator, b.begin().
  2. The default transform multiplies the matching pair at each position: 1*4, 2*5, and 3*6 give 4, 10 and 18.
  3. The default reduce adds them to 0, so the result is 4 + 10 + 18 = 32.

Example 3 — Count Positions That Match

Use std::equal_to as the transform and std::plus as the reduce to count equal pairs.

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

int main() {
    std::vector<int> a = {1, 2, 3, 4, 5};
    std::vector<int> b = {1, 0, 3, 0, 5};

    int same = std::transform_reduce(
        a.begin(), a.end(), b.begin(), 0,
        std::plus<>(),
        std::equal_to<>());

    std::cout << "matching positions: " << same << '\n';
    return 0;
}

How It Works

  1. Both vectors are compared position by position, so a[0] goes with b[0], a[1] with b[1], and so on.
  2. std::equal_to<>() is the transform step. It gives true or false for each pair: true false true false true.
  3. std::plus<>() adds them to 0. A true counts as 1, so three matches give 3.

Frequently Asked Questions

It applies a transform function to every element (or pair of elements) and reduces the results into one value, all in one pass.
Call std::transform_reduce(a.begin(), a.end(), b.begin(), 0). By default it multiplies matching elements and adds the products. The older way is std::inner_product().
They give the same dot product. transform_reduce() is C++17, may reorder the work, and supports execution policies. inner_product() is older and strictly in order.
No. The transform result goes straight into the reduce step, which is an advantage over transform() followed by accumulate().
The type of init decides the result type. Use 0LL for big sums or 0.0 for decimals. Also make sure the reduce operation is associative and commutative, because the order of work is not fixed.

Back to the function index

See all 14 <numeric> functions grouped by what they do.

Function index →

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