Write a non-cat blog post.
Defined
The lambda expression is defined as follows
[Capture] (parameters) mutable->return-type{statement}
Adult speech is
1.[capture]: Capture list. The capture list always appears at the beginning of the lambda function. In fact, [] is a lambda balloon. The compiler determines whether the next code is a lambda function according to the balloon. The capture list captures variables in the context for use by the lambda function;
2. (parameters): Parameter list. is consistent with the argument list of the normal function. If parameter passing is not required, it can be omitted together with parentheses "()";
The 3.mutable:mutable modifier. By default, a lambda function is always a const function, and mutable can cancel its constant behavior. When the modifier is used, the argument list cannot be omitted (even if the argument is empty);
4.->return-type: return type. Declares the return type of a function in the form of a trace return type. We can also omit the symbol "--" when no return value is required. In addition, if the return type is explicit, you can omit the part and let the compiler deduce the return type;
5.{statement}: Function body. The content is the same as a normal function, but you can use all the captured variables in addition to the parameters you can use.
En, that's about it.
Let's write a few programs to practice practiced hand ~ ~ ~
1. Simple sorting
#include <cstdio> #include <algorithm>int a[10]={0,1,2,3,4,5,6,7,8,9};int main () {Std::sort (a,a+10,[] ( int X,int y) {return x>y;}); for (register int i=0;i<10;i++) printf ("%d\n", A[i]); return 0;}
First-in lambda expression (mostly c++11)