C/C++ Arena

Step 2 of 6

Lambdas

A lambda is an unnamed function you write inline:

auto square = [](int x) { return x * x; };
square(5);   // 25

Lambdas shine as arguments to algorithms. Here std::sort takes a third argument: a comparison that says whether a should come before b.

Your turn: sort the words by length, shortest first.

Previous: sort and reverse Next: find_if, count_if, any_of