Numeric Algorithms accumulate reduce transform_reduce
Aggregate numeric ranges with accumulate, reduce, and parallel-friendly transform_reduce.
Numeric Algorithms accumulate reduce transform_reduce is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 = 15Custom Reduction
Provide an initial value and a binary operation.
int product = std::accumulate(v.begin(), v.end(), 1,
std::multiplies<int>{});
// product = 120Initial 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 6std::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:
reducemay run in parallelreducedoes 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 = 32std::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.
Frequently asked questions
Is the “Numeric Algorithms accumulate reduce transform_reduce” lesson free?
Yes — the full text of “Numeric Algorithms accumulate reduce transform_reduce” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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
- Non-Modifying Algorithms find count all_of
- Modifying transform copy_if replace
- Sorting and Partitioning sort stable_partition
- Numeric Algorithms accumulate reduce transform_reduce