lambda

An anonymous function object defined inline. Captures variables from the enclosing scope by value or reference.

Syntax

cpp
[capture](params) -> returnType { body }

Example

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

int factor = 3;
auto multiply = [factor](int x) { return x * factor; };

vector<int> v = {3, 1, 4, 1, 5};
sort(v.begin(), v.end(), [](int a, int b) { return a < b; });