C++ Academy · Lesson

Numeric Algorithms accumulate reduce transform_reduce

Aggregate numeric ranges with accumulate, reduce, and parallel-friendly transform_reduce.

Lesson 4 of 414 steps

Numeric Algorithms accumulate reduce transform_reduce is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

The <numeric> Header

Numeric algorithms operate on ranges of numbers. They live in <numeric>.

std::accumulate

The classic reduction. Sums by default — pass a custom binary operation for other reductions.

#include <numeric>
std::vector<int> v = {1, 2, 3, 4, 5};
int sum = std::accumulate(v.begin(), v.end(), 0);
// sum = 15

Custom Reduction

Provide an initial value and a binary operation.

int product = std::accumulate(v.begin(), v.end(), 1,
    std::multiplies<int>{});
// product = 120

Initial Value Type Matters

The accumulator type is deduced from the initial value. Provide a 0.0 to accumulate doubles into a double.

std::vector<double> v = {1.5, 2.5, 3.5};
double sum_d = std::accumulate(v.begin(), v.end(), 0.0);    // 7.5
int    sum_i = std::accumulate(v.begin(), v.end(), 0);      // truncates to 6

std::reduce (C++17)

Like accumulate, but the operation must be associative and commutative — allowing parallel evaluation.

#include <numeric>
#include <execution>
std::vector<int> v(1000);
int sum = std::reduce(std::execution::par,
    v.begin(), v.end(), 0);

reduce vs accumulate

Two differences:

  • reduce may run in parallel
  • reduce does not guarantee operation order — fine for + and *, broken for non-associative ops like string concatenation

std::transform_reduce (C++17)

Combine a transformation and a reduction in one pass. Faster and more expressive than separate transform + reduce.

// Dot product
std::vector<int> a = {1, 2, 3}, b = {4, 5, 6};
int dot = std::transform_reduce(
    a.begin(), a.end(), b.begin(),
    0,
    std::plus<int>{},          // reduce
    std::multiplies<int>{}     // transform
);
// dot = 1*4 + 2*5 + 3*6 = 32

std::inner_product

The older sibling of transform_reduce. Computes the inner product (dot product) of two ranges. Less general but simpler API.

std::partial_sum

Write running totals to an output range.

std::vector<int> v = {1, 2, 3, 4};
std::vector<int> sums(4);
std::partial_sum(v.begin(), v.end(), sums.begin());
// sums = {1, 3, 6, 10}

std::adjacent_difference

The opposite of partial_sum — write differences between consecutive elements.

std::vector<int> v = {1, 3, 6, 10};
std::vector<int> diffs(4);
std::adjacent_difference(v.begin(), v.end(), diffs.begin());
// diffs = {1, 2, 3, 4}

Floating Point Caveat

For floats, accumulation order matters because of rounding. reduce may give slightly different results than accumulate due to its unspecified evaluation order.

Real-World Use Cases

Numeric algorithms power:

  • Sum and product calculations
  • Statistical aggregates (mean, variance)
  • Dot products and norms in linear algebra
  • Cumulative sums for prefix queries

Quick Check

Which C++17 algorithm parallel-reduces a range with a single function call?

Recap

<numeric> provides accumulate, reduce, transform_reduce, partial_sum, and adjacent_difference for numeric reductions and prefix operations. Choose reduce for parallelism, accumulate for deterministic order.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Numeric Algorithms accumulate reduce transform_reduce” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Numeric Algorithms accumulate reduce transform_reduce”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Numeric Algorithms accumulate reduce transform_reduce”?

Aggregate numeric ranges with accumulate, reduce, and parallel-friendly transform_reduce. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Numeric Algorithms accumulate reduce transform_reduce” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Non-Modifying Algorithms find count all_of
  2. Modifying transform copy_if replace
  3. Sorting and Partitioning sort stable_partition
  4. Numeric Algorithms accumulate reduce transform_reduce
← Back to C++ Academy