C++11 introduces lambda expressions that allow programmers to define anonymous functions, which are executed at once, making it easy to program and prevent others from accessing them.
The syntax for lambda expressions is described by:
This assumes that we have defined a lambda expression like this. Now, what do you mean by the numbered parts on the way?
- The introduction of lambda expressions, in ' [] ' can be filled in ' = ' or ' & ' to indicate the lambda expression "capture" (lambda expression in a certain scope can be accessed by the data) in what way captured, ' & ' means a reference; ' = ' indicates capture by value, unless specifically indicated.
- A list of parameters for a lambda expression
- Mutable identification
- Exception ID
- return value
- The "function" body, which is the actual action required by the lambda expression
Add the complete code snippet:
int x = 10;
int y = 3;
int z;
z = [=] () mutable throw () int {int n = x + y; x = y; y = n; return n;} ();
cout<<z<<endl;
cout<< "x:" <<x<< "\ T" << "y:" <<y<<endl;
The result of the operation is:
13
X:10 Y:3
Because x, y is accessed as a value, the value of x, Y is not changed
Now that we have some understanding of the basic syntax of lambda expressions in our team, here are a few examples.
First, this example shows how to pass parameters to a lambda expression:
#include <iostream>
using namespace Std;
int main ()
{
int n = [] (int x, int y) {return x + y;} (5, 4);
cout << n << Endl;
}
Operation Result: 9
As we can see through this example, the arguments are passed in by the ' () ' after the function body.
The next example shows that you can use a lambda expression like a function call, but it's almost like the definition and invocation of a normal function, just learning how to use it.
#include <iostream>
using namespace Std;
int main ()
{
Auto F = [] (int x, int y) {return x + y;};
cout << F (+) << Endl;
}
Operation Result: 33
Lambda expressions are used in conjunction with STL algorithms, which are often used to sort and output arrays when writing test code, and are more convenient by the following algorithms:
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace Std;
int main ()
{
int a[10] = {0};
Srand (Time (NULL));
Generate (a,a+10,[] ()->int {return rand ()% 100;});
cout<< "before sort:" <<endl;
For_each (A, a+10, [&] (int i) {cout<< i << "";});
cout<<endl;
cout<< "after sort" <<endl;
Sort (a,a+10);
For_each (A, a+10, [&] (int i) {cout<< i << "";});
return 0;
}
Nesting of lambda expressions:
#include <iostream>
int main ()
{
using namespace Std;
int m = [] (int x)
{return [] (int y) {return y * 2;} (x) + 3; } (5);
cout << m << Endl;
}
Run Result: 13
The above code can be successfully compiled on both VC10 and VC11. The sense lambda expression is also a more interesting syntax and the first VC11 extension I've touched.
Http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438176.html
Http://www.cnblogs.com/zhuyp1015/category/370450.html
c++11 lambda expression (19 articles c++11)