Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Checkpoint: One Transform, Whole Answer

โญโญ intermediateโณ 35 min read๐Ÿ“ Lesson 101 of 204

Running averages with a single transform + captured prefix sum โ€” stateful lambdas done right.

running_avg is the module's exam because it combines everything: a std::transform that writes into a pre-sized output, a lambda capturing a running prefix by reference, and correct division by the count seen so far โ€” not the total size.

The by-reference capture is the load-bearing decision: the lambda must remember state between calls. The classic wrong answer divides the prefix by v.size() every time โ€” it looks plausible and fails exactly one test.

Before you code: what does the empty vector return? Decide, then make the test prove it.