vector< int> Vec; 1. Simple lambda Auto it = std::find_if (Vec.begin (), Vec.end (), [] (int i) {return i > 50;});
Class A {public:bool operator (int i) const {return i > 50;} }; Auto it = std::find_if (Vec.begin (), Vec.end (), A ()); 2. Lambda return syntax std::function< int (int) > square = [] (int i)-int {return i * I;} 3. Lambda expr:capture of local variable {int min_val = 10; int max_val = 1000; Auto it = std::find_if (Vec.begin (), Vec.end (), [=] (int i) {return i > Min_val && i < max_val; }); Auto it = std::find_if (Vec.begin (), Vec.end (), [&] (int i) {return i > Min_val && i < max_val; }); Auto it = std::find_if (Vec.begin (), Vec.end (), [=, &max_value] (int i) {return i > min_val && i &l T Max_val; }); }///4. Lambda Expr:capture of class member class A {public:void dosomething (); Private:std::vector<int> M_vec; int m_min_val; int M_max_va; }; 4.1 Capture member by This void A::D osomething () {Auto it = std::find_if (M_vec.begin (), M_vec.end (), [this] (int i) {return I & Gt M_min_val && i < m_max_val; }); }///4.2 capture member by default Pass-by-value void A::D osomething () {Auto it = std::find_if (M_vec.begin (), M_vec.end (), [=] (int i) {return i > M_min_val && i < M_max_val;}); }//4.3 capture member by default pass-by-reference void A::D osomething () {Auto it = std::find_if (m_vec.begi N (), M_vec.end (), [&] (int i) {return i > M_min_val && i < M_max_val;}); }
The above example basically covers the basic usage of lambda expression. We analyze each example one by one (the label is consistent with 1,2,3,4 in the code comment above):
- (1) This is the simplest lambda expression, it can be considered that the find_if of the lambda expression and the find_if using functor below are equivalent
- (2) This is a lambda expression with a return value, the syntax for the return value is shown above, and is written in the argument list after the parentheses. The return value can be omitted under the following conditions:
A. When the return value is void
B. The body of the lambda expression has return expr, and the type of expr is the same as the return value
- (3) This is an example of the lambda expression capture local variables, here are three small examples, the capture when the different syntax, the first small example = the capture of the variable pass-by-value, the second small example & Pass-by-reference, a third small example of the capture's variable, specifies the default pass-by-value, but max_value this individual pass-by-reference
- (4) This is an example of a lambda expression capture class member variable, and here are three small examples. The first small example is the capture member variable through the this pointer, the second to third is by default way, but the second is through the Pass-by-value way, the third is through the pass-by-reference
After analyzing the above example, let's summarize some considerations when using lambda expressions:
- (1) Lambda expressions to use reference variables are subject to the following principles:
A. Local variables in the invocation context can only be referenced by capture (as shown in example 3 above)
B. Non-local local variables can be directly referenced
- (2) The user needs to be aware that the variables (primarily pointers and references) referenced by closure (the callable entity generated by the lambda expression) must be guaranteed to be available until the closure call is complete, consistent with the callable entities generated after the bind parameter above.
- (3) about the use of lambda is used to generate closure, and closure is also a callable entity, so you can std::function object to save the generated closure, you can also directly with auto
Through the above introduction, we basically understand the use of function, bind and lambda, combine the three, C + + will become very powerful, a bit of functional programming taste. Finally, here's a little bit more, for generating function with bind and using lambda expressions to generate function, normally both are OK, but when there are many parameters, bind will pass in many std::p laceholders, And it looks as if there is no lambda expression intuitive, so it is generally advisable to prioritize using lambda expressions.
Closure is a collection of functions and the context of a non-local variable that it references. From the definition we can tell that closure can access variables beyond its definition, that is, the non-local vriables (non-local variable) mentioned above, which greatly increases its skill. The most important application for closure is the callback function
C++11 Lambda