An example of a lambda expression in C + +

Source: Internet
Author: User

Test environment Windows 7 vs2013

Lambda expressions in C + + 11 are used to define and create anonymous function objects to simplify the programming effort. For example, GPU programming is commonly used to.

One of the simplest Lamada expression programs

#include <functional> #include <iostream>using namespace Std;int main () {function<void (void) > Fun = [ ] () {cout << "Hello lambda" <<endl;};    You can also use the Auto variable fun ().    Cin.get (); return 0;}

Using in the STL

#include <functional> #include <vector> #include <algorithm> #include <iostream>using namespace Std;int main () {vector<int> vec_int;vec_int.push_back (1); Vec_int.push_back (2); Vec_int.push_back (3) ;//function<void (int) >  can also use autofunction<void (int) >  print_fun = [] (int item) {Cout<<item <<endl; }; int  Item function parameter For_each (Vec_int.begin (), Vec_int.end (), print_fun); Cin.get (); return 0;}

The syntax for Lambda is as follows:

[function Object Parameters] (operator overloaded function parameter) mutable or exception declaration, return value type {function Body}

As you can see, lambda is divided into five main parts:

I. [function object parameter], which identifies the beginning of a lambda, must exist and cannot be omitted. A function object parameter is a constructor that is passed to the compiler's automatically generated function object class.

The function object parameter can only use local variables that are visible to the scope of the lambda in which it is defined (including the this of the class that the lambda resides in). Function object parameters have the following form:
1, empty. No function object parameters are used.
2, =. The body of the function can use all the visible local variables within the scope of the lambda's scope ( including this of the class that the lambdais in), and it is the way the value is passed (equivalent to the compiler automatically passing all local variables for us by value).
3, &. The body of the function can use all the visible local variables within the scope of the lambda's scope (including this of the class that the lambda is in), and it is a reference delivery method (equivalent to the compiler automatically passing all local variables for us by reference).
4, this. The body of a function can use member variables in the same class as lambda.
5, A. The A is passed by value. When passing by value, the body of a function cannot modify the copy of a passed in because the function is const by default. To modify a copy of a passed in, you can add the mutable modifier.
6, &a. The A is passed by reference.
7, A, &b. The A is passed by value and B is passed by reference.
8, =, &a, &b. In addition to a and B are passed by reference, other parameters are passed by value.
9, &, A, B. Except for A and B are passed by value, other parameters are passed by reference.

#include <functional> #include <iostream>using namespace Std;class test{public:int num;public:test () {num = 100;} void Lambda_test () {int i_a = 3;int I_b = 5;auto fun1 = [] (int v) {cout <<v<<endl;};//does not capture any parameters, where int v is equivalent to a function parameter Number//Auto FUN1 = [] (int v) {cout << this->num<<i_a<<endl;}; Error: The enclosing function local variable cannot be referenced in the lambda body, unless it is in the Capture list fun1 (i_a),//through the function call//= according to the copy reference this, there are all local variables of the current block statement, can not be assigned, but can read auto fun2 = [=] () { cout << "num:" <<this->num << "i_a:" <<i_a<< "I_b:" <<i_b<<endl;}; FUN2 ();//& operates local variables by reference, this, can be assigned, can read auto fun3 = [&] () {this->num = $; i_a = ten; i_b = 20;}; Fun3 (); cout << "Modified num:" << this->num << "i_a:" << i_a << "i_b:" << i_b << endl;//Specify the parameters i_a readable writable, I_b read-only, This->num cannot access auto FUN4 = [&i_a, I_b] () {i_a =; cout << "i_a:" << i_a lt;< "I_b:" << i_b << Endl;}; Fun4 ();}}; int main () {Test test;test.lambda_test ();Cin.get (); return 0;} 

Second, (operator overloaded function parameter), identifies the parameter of the overloaded () operator, this part can be omitted when there is no parameter. Parameters can be passed by value (such as: (A, b)) and by reference (for example: (&a, &b)). Similar to a function is not an example.
Third, mutable or exception declaration, this part can be omitted. When you pass a function object parameter by value, you can modify the copy passed in by value (Note that you can modify the copy rather than the value itself) when you add the mutable modifier. The exception declaration is used to specify the exception that the function throws, such as an exception that throws an integer type, and you can use throw (int).

#include <functional> #include <iostream>using namespace Std;int main () {int num = 10; Mutable Note that it is possible to modify the copy instead of the value itself auto fun = [num] () mutable throw (), int{num = one; cout << "Lambda num:" << n Um << Endl;    };    Fun ();    cout << "num:" << num << Endl;    Cin.get (); return 0;}

The return value type, which identifies the type of the function return value, can be omitted when the return value is void, or there is only one return in the function body where the compiler can infer the return value type automatically.
Five, {function Body}, identifies the implementation of the function, this part cannot be omitted, but the function body can be empty.

#include <functional> #include <iostream>using namespace Std;int main () {//auto fun1 = [] (double A) double{A + = 1.1;; return A;}; COUT<<FUN1 (1.2) <<endl;auto fun2 = [] (double A)->decltype (a) {a + = 1.1;; return A;}; COUT<<FUN2 (1.2) <<endl;cin.get (); return 0;}


Reference: http://www.cnblogs.com/hujian/archive/2012/02/14/2350306.html

Copyright NOTICE: Welcome to reprint, if there are deficiencies, please treatise.

An example of a lambda expression in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.