Click the blue text
Follow us
Due to changes in the public account’s push rules, please click “View” and add “Star Mark” to receive exciting technical shares immediately
Source from the internet, infringement will be deleted
1. Definition
A lambda expression is a function (anonymous function), which is a function without a name. Why is there no need for a function name? Because we use it directly (once), embedded in it, and there is no need to call it from elsewhere.
A lambda expression is also called a closure. Closure means that it is closed (meaning it is not called from elsewhere), and package means function.
A lambda expression is actually a function object, which internally creates a class that overloads the () operator.
The simple syntax of a lambda expression is as follows: [capture] (parameters) -> return value { body }, only [capture] capture list and { body } function body are required, others are optional.
2. The simplest lambda expression (call)
int main()
{
[] {}(); // Three parts: []: represents the start of the lambda expression; {}: represents the function body, which contains nothing; (): represents the function call.
}
Equivalent to:
void f()
{
}
int main()
{
f();
}
Like other functions, we need a parameter list: (). So: [] {}(); can also be written as:
[](){}();
The second position adds a () to represent function parameters. If there are no parameters, () can be omitted.
3. Adding some print statements:
#include <iostream>
using namespace std;
int main()
{
[] { cout << "Hello, World!" << endl; }();
}
It can also be done like this:
#include <iostream>
using namespace std;
int main()
{
auto lam = [] { cout << "Hello, World!" << endl; };
lam();
}
4. Return Value
-> int: indicates that this anonymous function returns int. In most cases, the return value of the lambda expression can be inferred by the compiler, so we do not need to specify the return type.
int main()
{
auto lam = []() -> int { cout << "Hello, World!"; return 88; };
//auto lam = []() { cout << "Hello, World!"; return 88; };// automatic inference of return value
auto ret = lam();
cout << ret << endl; // outputs 88
auto lam2 = []() -> string { cout << "Hello, World!"; return "test"; };
auto ret1 = lam2();
cout << ret1 << endl; // outputs test
}
5. Capturing Variables
Variable capture is the secret to lambda’s excellence.
[] does not capture any variables, in this case, the lambda expression cannot access external variables.
[&] captures all variables by reference
[=] captures all variables by value (may be optimized by the compiler to const &)
[=, &foo] captures variable foo by reference, but the rest are captured by value; [&, foo] captures foo by value, but the rest are captured by reference; [bar] captures bar by value; does not capture other variables; [this] captures the this pointer of the class (widely used in Qt, so this lambda can access the data of the interface controls through this)
int a=1,b=2,c=3;
auto lam2 = [&,a](){ // b, c captured by reference, a captured by value.
b=5;c=6; // a =1; a cannot be assigned
cout << a << b << c << endl; // outputs 1 5 6
};
lam2();
6.
Undoubtedly, one of the biggest advantages of lambda is when using the STL algorithms library:
vector<string> address{"111","222",