#include <iostream>using namespace Std;void func1 () {cout<< "lambda expression, value capture, similar value passing" <<endl; size_t v1=30;//size_t is a unsigned int unsigned integer size_t v2=30; size_t v3=30; Auto F=[v1,v2,v3]{return v1+v2+v3;};/ The/LAMBDA expression [] contains the parameters to be passed, which can be put in more than one. v1=2; Cout<<f () <<endl; cout<<endl; Because this is a similar value pass, V1 has given F before the change, so it will not change. Note that the implicitly captured variable cannot be changed, only using its value cannot be changed//can be understood as [const V1] Of course you can't write this just so you can think}void Func2 () {cout<<endl; cout<< "lambda expression, reference capture, similar to reference passing" <<endl; size_t v1=30;//size_t is a unsigned int unsigned integer size_t v2=30; size_t v3=30; Auto F=[&v1,v2,&v3]{return v1+v2+ (v3++);};/ The/LAMBDA expression [] contains the parameters to be passed, which can be put in more than one. Here V1,v3 for reference, V2 for value cout<< "reference value unchanged before:" <<endl; cout<< "v1=" <<v1<<endl << "v2=" <<v2<<endl << "v3=" <<v3<<e Ndl Cout<<f () <<endl; v1=40; cout<< "Reference value changed:" <<endl; cout<< "v1=" <<v1<<endl << "v2=" ≪<v2<<endl << "v3=" <<v3<<endl;//called once F so V3 will add 1 cout<<f () <<endl; cout<<endl; Since this is similar to a reference pass, the value returned by the V1 change will also correspond to the change. And in F also quoted with V3 and V3 for the self-increment operation, so the value of the second output V3 is 31}void func3 () {cout<<endl; cout<< "lambda expression, implicit value capture" <<endl; size_t v1=30;//size_t is a unsigned int unsigned integer size_t v2=20; size_t v3=10; Auto F=[=,&v2] {v2=v1+v3; }; The lambda expression [] contains the arguments to be passed, which can be put in more than one. The first is =, the second is &v2, which means that except that V2 is a reference pass, other arguments from the outside are value passing, while implicit passing can only be used in one. Either the reference or the value pass/value pass can only be used, cannot be changed, if the attempt to change the value of the value passed by the compiler will be error cout<< "unchanged before:" <<endl; cout<< "v1=" <<v1<<endl << "v2=" <<v2<<endl << "v3=" <<v3<<e Ndl cout<< "After change:" <<endl; f (); cout<< "v1=" <<v1<<endl << "v2=" <<v2<<endl << "v3=" <<v3<<e Ndl Cout<<endl;} void Func4 () {cout<<endl; cout<< "lambda expression, hidden-type reference capture "<<endl; size_t v1=30;//size_t is a unsigned int unsigned integer size_t v2=20; size_t v3=10; Auto F=[&,v2] {v1++; V3=v2; }; The lambda expression [] contains the arguments to be passed, which can be put in more than one. The first is &, and the second is V2, which means that except that V2 is a value pass, other arguments from the outside are reference passes, while implicit passing can only be used at the same time. Either reference or Value pass cout<< "unchanged before:" <<endl; cout<< "v1=" <<v1<<endl << "v2=" <<v2<<endl << "v3=" <<v3<<e Ndl cout<< "After change:" <<endl; f (); cout<< "v1=" <<v1<<endl << "v2=" <<v2<<endl << "v3=" <<v3<<e Ndl Cout<<endl;} void Func5 () {cout<< "lambda expression, variable value capture" <<endl; size_t v1=30;//size_t is a unsigned int unsigned integer size_t v2=20; size_t v3=10; Auto F=[v1,v2,v3] () mutable {v1=v2+v3; V2=v1+v3; V3=v1+v2; }; The lambda expression [] contains the arguments to be passed, which can be put in more than one. cout<< "v1=" <<v1<<endl << "v2=" <<v2<<endl << "v3=" <<v3<<endl; cout<<endl; Is it strange to see the output, not to say mutable, the result is the same. The variable here means that the parameter passed in the lambda expression can only be obtained by the value. It is not possible to perform other operations on//mutable, so that it can be used like a variable in a function, and can be interpreted as a value passed in a function. The value passed in a function is any action that can be made on its arguments. }void Func6 () {cout<< "lambda expression, specifying return type" <<endl; Auto f=[] (int i)->int {return i; }; The lambda expression [] contains the arguments to be passed, which can be put in more than one. Cout<<f (<<endl;) To specify the return type of a lambda expression we have to set the cout<<endl; }void Main (void) {func1 ();//value Capture Func2 ();//reference Capture FUNC3 ();//implicit value passing FUNC4 ();//implicit reference passing FUNC5 ();//variable Lambda func6 ();//Specifies the return type system ("Pause");}
If there's anything wrong, please point out that the basic usage of the lambda expressions that have been sorted out these days
c++11 lambda expression Basic usage