C++11--LAMBDA-expression

Source: Internet
Author: User

lambda expression

A syntax for functional programming has the following advantages:
(1) Declarative programming Style: in-place anonymous definition of the target function or function object, do not need to write an additional named function or function object. Write programs in a more straightforward way, with good readability and maintainability.
(2) Simplicity: No additional functions or function objects are required to avoid code bloat and functional dispersion.
(3) Implement functional closures at the time and place needed to make the program more flexible.

Basic concepts and usage of lambda expressions

A lambda expression defines an anonymous function and can capture a range of variables. The syntax for a lambda expression is as follows:
[capture] (params) opt -> ret {body;}
Among them, capture is the capturing list; params is the parameter table; Opt is the function option; RET is the return value type; body is the function body. A capture list is a value type or reference type that introduces some variables outside of lambda into the lambda interior.

    Auto F = [] (int a), int {return a + 1;};    Std::cout << F (1) << Std::endl;

or omit the return value type (c++11 can do type deduction) and write directly

    Auto F = [] (int a) {return a+1;};    Std::cout << F (1) << Std::endl;

Capture List

A lambda expression can capture a range of variables through a capture list:

[] do not capture any variables
[&] captures all variables in the outer scope and uses them as references in the body of the function (captured by reference)
[=] Captures all variables in the outer scope and uses them as replicas in the body of the function (captured by value)
[=, &foo] Captures all variables in the outer scope by value and captures the Foo variable by reference
[Bar] captures bar variables by value without capturing other variables
[This] captures the this pointer in the current class, allowing the lambda expression to have the same access as the current class member function. If you have already used = or &, the item is added by default. The purpose of this capture is to use the current class's member functions and member variables in the lambda.

    int a = 0, b = 1;    Auto F1 = []{return A;};//error, no external variable captured    auto F2 = [&]{return a++;};/ /captures references to external variables, and can be implemented by a++    auto F3 = [=]{return A;};    Auto F4 = [=]{return a++;};/ /error, A is a replica, and is a const type and cannot be modified by    auto F5 = [A, &b]{return A + (b++);} Yes, catch a by value, and capture B by reference

It is important to note that by default, lambda expressions cannot modify external variables that are captured by means of replication. If you want to modify these external variables, you can capture them by reference, or by explicitly indicating that the lambda expression is of type mutable:

    int a = 0;    Auto F1 = [=]{return a++;};/ /error    Auto F2 = [=] () Mutable{return a++;}//Note that the mutable-modified lambda should also write the parameter list    int main () {    int a = 1    , even if there are no parameters; Auto F = [=]{return a+1;};    cout << F () << Endl; Output 2    cout << "a =" << a << Endl; A = 1    auto F1 = [=] () Mutable{return a++;};    cout << "a =" << a << Endl; A = 1    cout << f () << Endl;//Output 2????    cout << "a =" << a << Endl; A = 1    return 0;    }

and need to be aware of the delayed invocation of lambda

  1. int a = 0;
  2. auto f = [=]{return a;};
  3. a += 1;
  4. std::cout << f() << std::endl; //输出的a为之前的a

Lambda expressions capture external variables by value, which are copied into F at the moment of capture.

lambda expressions and closures

In c++11, Lambda is a closure type and is a special, anonymous, non-union class type. You can think of a lambda as a class with operator (), which is an imitation function. You can therefore use Std::function and std::bind to store and manipulate lambda expressions.

    Std::function<int (int) > f1 = [] (int a) {return A;};    Std::function<int (void) > F2 = Std::bind ([] (int a) {return A;}, 123);

For lambda expressions that do not capture any variables, they can also be converted to a normal function pointer:

    Using func_t = Int (*) (int);    func_t f = [] (int a) {return A;};    F (123);

You can think of a lambda as a class with operator (), which is an imitation function. According to the C + + standard, the operator () of a lambda expression is const by default, so you cannot modify the value of an external variable while capturing by value, while using mutable you can cancel the const of operator ().

In -place declaration and use of lambda expressions
int count = count_if (Coll.begin (), Coll.end (), [] (int x) {return x > 5 && x < 10;});

+

C++11--LAMBDA-expression

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.