C++11 standard: Lambda expression explanation

1. Definition analysis

      Lambda expression, as the name suggests, is an expression. The difference from regular function expressions is that it is "anonymous", that is, expressions without function names (anonymous expressions for short).

 

2. The general expression of Lambda

capture list : The capture list is a list of local variables defined in the function where the lambda is located (usually empty)

parameter list : parameter list

return type : return type

function body : function body

Explanation: (1) From the appearance point of view, similar to ordinary functions, Lambda expressions have a return type, a parameter list, and a function body; the difference is that it lacks the function name and adds a capture list. At the same time, Lambda must use tail return to specify the return type. Let's explain through a few examples

(2) Unlike the function body, Lambda may be defined inside the function

(3) We can ignore the parameter list and return type, but must always include the capture list and function body (the capture list can be empty, but'[ ]'cannot be omitted)

3. Examples

(1) Introduction to Lambda

It can be seen that the calling method of lambda, like ordinary functions, is the calling operator.

In this lambda, ignoring the parameter list in parentheses is equivalent to specifying an empty parameter list. In this example, when f is called, the parameter list is empty. If you ignore the return type, Lambda infers the return type based on the code in the function body. If the function body has only one return statement, the return type is inferred from the type of the returned expression. Otherwise, the return type is empty

(2) Passing parameters to Lambda

Let's try it with the sorting algorithm:

Among them, the capture list is empty, and a and b represent formal parameters. When the stable_sort function is called, the parameters are passed to a and b when traversing the elements of the container words . According to the operation rules in the lambda expression, it returns true or false to determine a, b. Who is the big one?

(3) Use value capture list

The above example has not used the capture list, here it is used when calling the find_if standard library function.

int sz = 2;

vector<string> words={"123", "23", "456"};

That is to say, the value of sz is obtained from the front (be careful not to exceed the scope of the variable), where the result of wc is to find the first element index greater than 2 in words.

 

The capture type of the capture list in the Lambda expression is divided into two types: value capture and reference capture. They are introduced separately below.

3.1 Value capture

 

3.2 Reference capture

(4) Multiple parameters

Our capture list can also be multi-parameters, or even stream operations (note that stream can only be passed by reference: the stream object contains a pointer to the IO buffer. If the stream object can be copied, then there will be two pointers operating the buffer at the same time , How to release and how to modify will have conflict synchronization problems, so the stream object cannot be copied.)

In addition to the explicit list of the capture parameters of the variables in the function we use above, you can also let it deduce which variables are used, that is, "implicit capture".

4. Implicit capture

There are also two ways of implicit capture, value ( = ) and reference ( & )

(1) Value capture

The result is that the value of sz will be found in the previously defined variable sz

(2) Citation capture

Similar value capture, no examples.

 

If you mix implicit and explicit capture:

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_38915078/article/details/112630844