c++ lambda
A lambda function in C++ is an anonymous function that can be defined inline. It is defined using the following syntax:
[ captures ] ( parameters ) -> return_type {
// function body
}
Where:
- captures
are variables captured from the enclosing scope.
- parameters
are the function parameters.
- return_type
is the return type of the lambda function.
- function body
contains the actual code of the lambda function.
Here's an example of a simple lambda function that takes two integers and returns their sum:
auto sum = [](int a, int b) -> int {
return a + b;
};
In this example:
- auto
is used to automatically deduce the return type of the lambda function.
- [](int a, int b)
defines the lambda function taking two integers.
- -> int
specifies that the return type is int
.
- return a + b;
is the function body that returns the sum of the two integers.