Lambda expression and 11lambda expression in c ++ 11
URL: http://www.cnblogs.com/archimedes/p/c11-lambda.html.
"Lambda expression" is an anonymous function. A lambda expression is named after the Lambda Algorithm in mathematics and corresponds directly to the lambda abstraction action. It is an anonymous function, that is, a function without a function name. Lambda expressions can represent closures (note that they are different from traditional mathematics ).
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}
Except for "[]" (the capture list can be blank) and "compound statement" (equivalent to the function body defined by the name function), the other functions are optional. Its type is the only non-union class type with the operator () member. It is called the closure type ).
In C ++, a lambda expression represents a callable code unit. We can understand it as an unnamed inline function.
Unlike normal functions, lambda must use the tail return function to specify the return type.
For example, to call std: sort and iso c ++ 98 in <algorithm>, you must first write a compare function:
Bool compare (int & a, int & B) {return a> B; // sort in descending order}
Then, call:
sort(a, a + n, compare);
However, the Lambda expressions added using the iso c ++ 11 standard can be written as follows:
Sort (a, a + n, [] (int a, int B) {return a> B ;}); // sort in descending order
In this way, the code is much more concise.
The Lambda type is unique and the corresponding object cannot be explicitly declared through the type name, but the auto keyword and type derivation can be used:
auto f = [](int a, int b){return a > b;});
A major difference from other languages is the use of Lambda and C ++ type systems, such:
Autof = [x] (int a, int B) {return a> x ;}); // copy int x = 0, y = 1; auto g = [&] (int x) {return + + y;}); // y is captured and referenced. After g is called, y is modified, note that the lifetime bool (* fp) (int, int) = [] (int a, int B) {return a> B ;}) of y ;}); // function pointer can be converted only when not captured
Lambda expressions can be nested.
The forthcoming iso c ++ 14 supports generic lambda expressions based on type inference. The preceding sorting code can be written as follows:
Sort (a, a + n, [] (const auto & a, const auto & B) {return a> B ;}); // sort in descending order: does not depend on the specific types of a and B
Because parameter types can be deduced like function template parameters without coupling with specific parameter types, it is helpful to refactor the code. Similar to the function of declaring variables using auto, it also allows you to avoid writing too complex parameter types. In particular, you do not need to explicitly specify the parameter type to make it easier to use higher-order functions.
The following is an example of using a Lambda expression:
#include<iostream>#include<algorithm>#include<vector>#include<ostream>using namespace std;int main(){ vector<int> v; for (int i = 0; i < 10; i++){ v.push_back(i); } for_each(v.begin(), v.end(), [](int n){cout << n << " "; }); cout << endl; return 0;}
The default return type of Lambda expressions is void.
For comparison, the following code uses function objects to implement the same functions:
#include<iostream>#include<algorithm>#include<vector>#include<ostream>#include<cassert>using namespace std;class LambdaFunctor{public: void operator()(int n) const{ cout << n << " "; }};int main(){ vector<int> v; for (int i = 0; i < 10; i++){ v.push_back(i); } for_each(v.begin(), v.end(), LambdaFunctor()); cout << endl; return 0;}
By comparison, we will find that using Lambda expressions is much simpler.
The Lambda expression mentioned above can operate on the variables in the scope, which needs to be implemented through a special syntax called "capture, it is to list the "external" variables to be captured in [], so that these variables can be accessed and operated in the function body. Refer to the following code:
# Include <iostream> # include <algorithm> # include <vector> # include <ostream> # include <cassert> using namespace std; int main () {vector <int> v; for (int I = 0; I <10; I ++) {v. push_back (I);} // use the for_each statement and Lambda expression to count dual elements. int evenCount = 0; for_each (v. begin (), v. end (), [& evenCount] (int n) {cout <n; if (n % 2 = 0) {cout <"is even" <endl; evenCount ++;} else {cout <"is odd" <endl ;}}); cout <"There are" <evenCount <"even numbers in the vector. "<endl; getchar (); return 0 ;}
References
Baidu encyclopedia
Visual C ++ 2010 authoritative Development Guide