C + + supplements--LAMBDA expressions
Preface
Sometimes, we need to use a function frequently within a function. At this point, we can write this function as a separate function. In fact, this new function is probably not required to be called elsewhere. We want to limit its scope, preferably limited to the current function. The interior of the function is not allowed to redefine other functions. To solve this problem, in the new standard, C + + introduces the concept of lambda expressions (lambda expression). With lambda expressions, C + + is a big step in a perfect language. In general, lambda expressions greatly enhance the ability of C + + to function.
lambda expression
The description of it in C++primer is "a lambda expression represents a callable unit of code that can be understood as an unnamed inline function." the general form of a lambda expression is
[Capture List] (parameter list)->return type {function Body}
- Capture List (required)
- Parameter list---parameter lists (optional)
- function body (required)
- return type (optional)
Here we Explore
Perceptual Knowledge
#include <iostream> #include <string>using namespace Std;int main () {//define a lambda expression auto print = [] (string s) {for (int i = 0; i < s.size (); i++) cout << s[i] << ""; cout << Endl;}; String man = "man"; string woman = "woman"; string say = "Iloveyoubaby";//call to print the same function print (man);p rint (woman);p rint ( say); Cin.get (); return 0;}
Run
Capture List (capturing lists)
indicate the local variables that need to be used in the capture list (global variables can be used directly, without capture). Two modes of capture: value capture, reference capture .
1. Value Capture
- The premise: the captured variables can be copied.
- Important: When you capture a value, by default, you cannot modify the value of a variable in the capture list unless you add the keyword mutable after the parameter list.
- The difficulty: The timing of the copy is when the lambda expression is defined, not when it is used.
Example
#include <iostream>using namespace Std;int g_data (1); int main () {//data definition must precede the lambda expression, otherwise the int data (2) cannot be captured; auto fun = [data] () mutable{//in a lambda expression, fix the value of data = 3; If not added mutable, here will errorg_data = 4;}; cout << "Fun () call before" << endl;cout << "g_data =" << g_data << ends << "data =" << ; Data << endl;//Fix data value data = 5;//called after fixup to verify value capture time fun (); cout << ' fun () after call << endl;cout << ' g _data = "<< g_data << ends <<" data = "<< data << endl;cin.get (); return 0;}
Run
2. Reference Captureadd & before the variable name, which is the reference capture. It is the same as the reference syntax. Example
#include <iostream>using namespace Std;int main () {cout << "reference capture Demo" << Endl;int data (0); cout << "F Un () call before "; cout <<" data = "<< data << endl;auto fun = [&data] () {data++; Because it is a reference capture, when modifying a value, mutable can call Fun () fun () without};//, cout << "fun () after call"; cout << "data =" << data << End L;cin.get (); return 0;}
Run
Implicit captureimplicit capture is not the third way to capture, it's just a way to write a capture list. several common ways of writing
- [&] All variables are captured by reference.
- [=] All variables are captured with a value.
- [&,identifier_list] Identifier_list is a comma-separated list of parameters that are captured using values, and others using references.
- [=,identifier_list] Identifier_list is a comma-separated list of parameters, the local variables in this list are captured using a reference, and the variable name needs to be added & before, and other usage values are captured.
Special, empty capture list []: Indicates that no local variables can be used in a lambda expression.
Example
Run
parameter list (parameter list)Parameter lists and common functions have the same argument list usage, except for one thing: there can be no default parameters. that is, it is not possible to have this notation: [] (int a=0) {};
function Body and return typethree-point rule:
- If there is only one return statement in the function body, the return type can be no, and the returned value types are inferred from the return statement.
- If there is no return statement, the return value type is void.
- If there are other statements besides the return statement, you must specify the type of the return value, and you must use the tail return type, which is the form of type-name.
C + + supplements--LAMBDA expressions