Lambda expressions in C++ 11 are used to define and create anonymous function objects to simplify programming. The syntax of Lambda is as follows: [function object arguments] (operator overloading function arguments) mutable or exception declaration -> return value type {function body} As you can see, Lambda is divided into five main parts: Function object arguments, (operator overloading function arguments), mutable or exception declarations, -> return value types, {function body}. The following are introduced respectively. The [function object argument], which marks the start of a Lambda, must exist and cannot be omitted. Function object arguments are passed to the constructor of the function object class automatically generated by the compiler. Function object arguments can use only local variables that are visible in the scope of the Lambda until the Lambda is defined (including this of the Lambda’s class). Function object arguments take the following form: 1. No function object arguments are used. 2, =. All visible local variables in the scope of the Lambda can be used inside the function body (including this of the class of the Lambda) in value passing (equivalent to the compiler automatically passing all local variables by value for us). 3, &. All visible local variables in the scope of the Lambda can be used inside the function body (including this of the class of the Lambda), and are passed by reference (equivalent to the compiler automatically passing all local variables for us by reference). 4, this. Member variables from the class of the Lambda can be used inside functions. 5, a. Pass a by value. When passed by value, the body of a function cannot modify the copy of a passed in, because the function is const by default. To modify the passed copy of A, add the mutable modifier. 6, & a. Pass a by reference. 7, A, & B Pass A by value and B by reference. 8, =, &a, &b. Except for a and b, which are passed by reference, all parameters are passed by value. 9, a, B. All parameters are passed by reference except a and b, which are passed by value. (operator overrides function arguments), the argument that identifies the overloaded () operator. If there are no arguments, this part can be omitted. Parameters can be passed either by value (e.g. :(a,b)) or by reference (e.g. :(&a,&b)). A mutable statement is a mutable statement. When passing function object arguments by value, the mutable modifier allows you to modify copies passed by value (note that copies can be modified, not values). The exception declaration is used to specify exceptions thrown by a function. For example, if an integer is thrown, throw(int) can be used. -> Return value type: identifies the type of the return value of the function. This part can be omitted if the return value is void, or if there is only one return in the function body (at which point the compiler can automatically infer the return value type). {function body}, identify the implementation of the function, this part can not be omitted, but the function body can be empty. \

Below is a sample code that illustrates each of the situations mentioned above, with simple comments for reference.

class CTest
{
public:
 CTest() : m_nData(20) { NULL; }
 void TestLambda()
 {
  vector<int>vctTemp; vctTemp.push_back(1); vctTemp.push_back(2); 1 2 {for_each(vcttemp.begin (), vcttemp.end (), [](int v){cout << v << endl; }); } // Pass all visible local variables in scope (including this) as values, output: 11 12 {int a = 10; for_each(vctTemp.begin(), vctTemp.end(), [=](int v){ cout << v+a << endl; }); } // Pass all visible local variables in scope (including this) by reference, output: 11 13 12 {int a = 10; for_each(vctTemp.begin(), vctTemp.end(), [&](int v)mutable{ cout << v+a << endl; a++; }); cout << a << endl; } // Pass the local variable a as a value, output: 11 13 10 {int a = 10; for_each(vctTemp.begin(), vctTemp.end(), [a](int v)mutable{ cout << v+a << endl; a++; }); cout << a << endl; } // Pass local variable a by reference, output: 11 13 12 {int a = 10; for_each(vctTemp.begin(), vctTemp.end(), [&a](int v){ cout << v+a << endl; a++; }); cout << a << endl; 22 {for_each(vcttemp.begin (), vcttemp.end (), [this](int v){cout << v+m_nData << endl; }); } // int a = 10; // int a = 10; int b = 15; for_each(vctTemp.begin(), vctTemp.end(), [=, &b](int v){ cout << v+a << endl; b++; }); cout << b << endl; } // Operator overload function arguments are passed by reference, output: 2 3 {for_each(vcttemp.begin (), vcttemp.end (), [](int &v){v++; }); for_each(vctTemp.begin(), vctTemp.end(), [](int v){ cout << v << endl; }); } // Empty Lambda expression {[](){}(); [] {} (); } } private: int m_nData; };Copy the code

\

\