Skip to content
Guest
Learn C++ · Course Syllabus Show lessons
The Standard Library (STL) · 8 min read · Intermediate

Lambdas & STL Algorithm Pipelines

Write concise inline predicates with lambda capture lists and combine std::transform, std::accumulate, and std::ranges.

What you'll learn

  • Understand lambda capture clauses ([], [x], [&x])
  • Aggregate values cleanly with std::accumulate from <numeric>
  • Sort custom structs with inline lambda comparators

C++ lambdas ([capture](params) { body }) compile into zero-overhead inline function objects. Pairing lambdas with <algorithm> and <numeric> (std::sort, std::accumulate, std::all_of) lets you express complex data transformations without hand-written index loops.

Fill-in-the-Blank Exercise

Lambda Capture & Accumulate

Capture min_score by value inside a lambda predicate.

int min_score = 80;
auto is_pass = [](int s) { return s >= min_score; };
std::cout << is_pass(85) << "\n";

Live Code Lab

C++lambdas_stl.cpp
Expected OutputClick “Run Live” to compile with GCC 13 (C++20)
Sum: 350 | >= 85: 3

Check Your Understanding

Score: 0 / 1

1. In a C++ lambda [x, &y](int n) { ... }, how are x and y captured?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind