This article address: http://www.cnblogs.com/archimedes/p/c11-lambda.html, reprint please indicate source address.
A lambda expression is an anonymous function , which is named after the lambda calculus in mathematics and directly corresponds to the one in which it is an anonymous function (lambda-abstraction). , which is a function without a function name. A lambda expression can represent closures (notice differs from the traditional mathematical sense).
A major highlight of the ISO C + + 11 standard is the introduction of lambda expressions. The basic syntax is as follows:
[Capture List] (parameter list)->return type {function Body}
In addition to "[]" (where the capture list can be empty) and "compound statement" (equivalent to the function body defined by the named function), the others are optional. Its type is the only non-federated class type that has member operator (), called the closure type (closure type).
In C + +, a lambda expression represents a unit of code that can be called. We can interpret it as an unnamed inline function.
Unlike a normal function, a lambda must use a trailing return to specify the return type.
For example: Call <algorithm> std::sort,iso C + + 98 is written to write a compare function first:
bool Compare (intint &b) { return a > b; // Sort Descending }
Then, call this again:
Sort (A, a + N, compare);
However, with the addition of a lambda expression to the ISO C + + 11 standard, you can write:
Sort (A, a + N, [] (intint b) {return a > B;}); // Sort Descending
In this way, the code is significantly more concise.
Because the type of the lambda is unique , the corresponding object cannot be explicitly declared through the type name, but can take advantage of the Auto keyword and type deduction:
Auto F = [] (intint b) {return a > B;});
One of the more obvious differences from other languages is the use of lambda and C + + type systems , such as:
autof = [x] (int A, int b) {return a > x;}); // x is captured copy int x = 0 , y = 1 ;auto g = [&] (int x) {return ++y;}); // y is captured by a reference, and when G is called, it modifies Y and needs to be aware of the lifetime of y bool (*FP) (int , int ) = [] ( int A, int b) { return a > B;}); // can only be converted to a function pointer when not captured
Lambda expressions can be used in a nested style.
The forthcoming ISO c++14 supports generic lambda expressions based on type inference. The sort code above can be written like this:
Sort (A, a + N, [] (constconst Auto &b) {return a > B;}); // Sort Descending: does not depend on the specific type of a and B
Because parameter types and function template parameters can be deduced without coupling to specific parameter types, which facilitates refactoring of code, and using auto to declare variables is similar, it also allows to avoid writing overly complex parameter types. In particular, it is not necessary to explicitly indicate that parameter types make it easier to use higher-order functions.
Here's an example of a simple use of a lambda expression:
#include <iostream>#include<algorithm>#include<vector>#include<ostream>using namespacestd;intmain () {vector<int>v; for(inti =0; I <Ten; i++) {v.push_back (i); } for_each (V.begin (), V.end (), [] (intN) {cout << n <<" "; }); cout<<Endl; return 0;}
The default return type for lambda expressions is void
For comparison, the following code uses the function object to implement the same functionality:
#include <iostream>#include<algorithm>#include<vector>#include<ostream>#include<cassert>using namespacestd;classlambdafunctor{ Public: void operator()(intNConst{cout<< N <<" "; }};intmain () {vector<int>v; for(inti =0; I <Ten; i++) {v.push_back (i); } for_each (V.begin (), V.end (), Lambdafunctor ()); cout<<Endl; return 0;}
By contrast, you'll find it much simpler to use lambda expressions.
The lambda expression mentioned above can manipulate variables in the scope of the action, which needs to be implemented by a special syntax called "Capture", by listing the "external" variables that will be captured in [], so that the variables can be accessed and manipulated within the body of the function. Refer to the following code:
#include <iostream>#include<algorithm>#include<vector>#include<ostream>#include<cassert>using namespacestd;intmain () {vector<int>v; for(inti =0; I <Ten; i++) {v.push_back (i); } //using For_each statements and lambda expressions to implement the count of dual elements intEvencount =0; For_each (V.begin (), V.end (), [&evencount] (intN) {cout<<N; if(n%2==0) {cout<<"is even"<<Endl; Evencount++; } Else{cout<<"is odd"<<Endl; } }); cout<<"there is"<< Evencount <<"even numbers in the vector."<<Endl; GetChar (); return 0;}
Resources
Baidu Encyclopedia
Guide to the development of Visual c++2010 Authority
Lambda expression in c++11