C++ std::reduce() Function

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

Definition

In C++17, std::reduce() is the relaxed cousin of std::accumulate(). It also folds a range into one value, but the standard lets it combine the elements in any order and in any grouping.

That freedom is what allows it to use several threads or SIMD when you pass an execution policy such as std::execution::par. The price is that your operation must be associative and commutative, like + on integers. You need <numeric> and a C++17 compiler.

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

Syntax

The shortest form takes only the range. You can add a start value, an operation, and an execution policy.

C++
auto a = std::reduce(first, last);
auto b = std::reduce(first, last, init);
auto c = std::reduce(first, last, init, op);
auto d = std::reduce(std::execution::par, first, last);
CallWhat it does
reduce(first, last)Adds all elements. The start value is T{}, which is 0 for numbers.
reduce(first, last, init)Adds all elements to init. The result has the type of init.
reduce(first, last, init, op)Combines the elements with op, which must be associative and commutative.
reduce(policy, first, last, ...)Same as above, but allowed to run in parallel. Needs <execution>.

On small vectors reduce() is not faster than accumulate(). Parallel runs pay off on large ranges.

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

template<class InputIt, class T, class BinaryOp>
T reduce(InputIt first, InputIt last, T init, BinaryOp op);

// Also: reduce(first, last), and every form with an ExecutionPolicy&&.

Parameters

ParameterDescription
policyOptional. An execution policy such as std::execution::seq, par or par_unseq.
first, lastRequired. Iterators that mark the range [first, last).
initOptional. The start value. If left out, T{} is used. Its type is the type of the result.
opOptional. A binary operation that is associative and commutative. The default is +.

Return Value

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

C++ Examples

Example 1 — Sum Without a Start Value

Call reduce() with just two iterators to add everything up.

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

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int sum = std::reduce(v.begin(), v.end());

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

How It Works

  1. The vector holds the numbers from 1 to 10.
  2. With only two arguments, reduce() starts from int{}, which is 0, and uses + as the operation.
  3. It may add the numbers in pairs or in any other grouping, but for integer addition every grouping gives the same total, 55.

Example 2 — reduce() with init and an Operation

You can give a start value and an operation. The operation must not depend on order.

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

int main() {
    std::vector<int> v = {10, 20, 30};
    std::vector<int> f = {1, 2, 3, 4, 5, 6};

    int shifted = std::reduce(v.begin(), v.end(), 100);
    int product = std::reduce(f.begin(), f.end(), 1,
                              std::multiplies<>());

    std::cout << "sum from 100: " << shifted << '\n';
    std::cout << "product: " << product << '\n';
    return 0;
}

How It Works

  1. The first call uses 100 as init. The total is 100 + 10 + 20 + 30, which is 160.
  2. The second call passes std::multiplies<>() and a start value of 1. The start value must be the identity of the operation, which is 1 for a product.
  3. Multiplication is associative and commutative, so the product of 1..6 is always 720, no matter how reduce() groups the numbers.

Example 3 — Parallel reduce() with std::execution::par

Add an execution policy as the first argument to allow several threads to share the work.

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

int main() {
    std::vector<int> v(1000);
    std::iota(v.begin(), v.end(), 1);   // 1, 2, ..., 1000

    long long total = std::reduce(std::execution::par,
                                  v.begin(), v.end(), 0LL);

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

How It Works

  1. std::iota() fills the vector with 1, 2, 3, ... 1000, so you do not have to type the numbers.
  2. std::execution::par tells reduce() it may split the range between threads. Each thread adds its own part, then the parts are added together.
  3. The start value 0LL makes the total a long long. The answer is 500500 on every run, because integer addition does not care about order.

Frequently Asked Questions

std::reduce() from <numeric> combines a range into one value, usually a sum. Unlike accumulate(), it may reorder the elements, so it can run in parallel with an execution policy.
Use reduce() when your operation is associative and commutative, such as + or * on integers, and you may want parallel speed on a large range. Use accumulate() when order matters, for example subtraction or string joining.
Floating-point addition is not exactly associative, so a different grouping can change the last digits. reduce() may pick any grouping.
It uses T{}, which is 0 for int and double. That is only right for addition. For a product pass 1.
You need #include <execution> and C++17. With GCC the parallel algorithms often also need the TBB library (-ltbb). MSVC needs nothing extra.

Next: transform_exclusive_scan()

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

transform_exclusive_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.

8 people found this page helpful