Lambda expression of c++11

Source: Internet
Author: User

Lambda expressions are derived from the concept of functional programming, which can be used to anonymously define a target function or function object, without the need to write an additional named function or function object. The type of a lambda expression is referred to as a "closure type" in c++11, and it can also be understood as an imitation function (with the operator () class), which has the following grammatical form:

[Capture] (params) opt-a ret {body;};

Capture: Capturing list;

Params: parameter list;

OPT: function options;

RET: return value type;

Body: function body.

A simple lambda expression is as follows:

 #include  " stdafx.h  "   #include  <iostream> //  Std::cout  int   main () {auto F  = [] (int  A, int  b), int  {return  ( A*b);    }; Std::cout  << F (3 , 4 ) << Std::endl; //     Output:12  return  0  ;}  

Capturing fields in lambda expressions are useful, capturing the idea that capturing an external variable is used inside the body of a lambda function , and there are several different ways to capture it:

[]: Do not capture any external variables, if the use of external variables in the function body will be error;

[&]: Capture all external variables as a reference, and if you change the value of an external variable within the function body, the value of the external variable will change accordingly;

[=]: Captures all external variables as a value, and the value of the external variable does not change if the external variable value is changed within the function body;

[=, &foo]: captures all external variables in a worthwhile way and captures the Foo variable in a reference way;

[Bar]: The bar variable is captured in a worthwhile way, and other external variables are not captured;

[This]: captures the this pointer in the current class so that the lambda expression has the same access rights as the current class member function. If & or = is already in use, this is added by default.

  Note the difference between the catch by value and the catch by reference, a copy of the external variable that is used in the lambda expression is captured by the value, and saved to the lambda closure type, and if the external variable is subsequently modified, the variable in the lambda expression will not change. Because the variable is just a copy, capturing by reference is directly reading or manipulating the value of the variable, as shown in the code:

#include"stdafx.h"#include<iostream>//Std::coutintMain () {intNumA =0; intNumB =0; {Auto F1= [] {returnNumA; };//error, no external variables capturedAuto F2 = [&numb] {return(--NumB);        };        F2 (); Std::cout<< NumB << Std::endl;//-1    }    intNUMC = -; intNUMD = -; Auto F3= [=] ()int{return(numc*NUMD);    }; NUMD= About; Std::cout<< f3 () << Std::endl;//output:10000    return 0;}
With lambda expressions, when we use a For loop or std::for_each, we can change to the following form:
#include"stdafx.h"#include<iostream>//Std::cout#include <list>#include<algorithm>intMain () {std::list<int> L = {1,3,5, the, -, -,0, -1, - -}; intNcount =0; Std::for_each (L.begin (), L.end (), [&ncount] (intVal) {if(Val > -) ncount++; }); Std::cout<<"The number larger than in L is:"<< ncount <<Std::endl; return 0;}

To summarize, the lambda expression looks like an anonymous function inside Java, and after using a lambda expression, you can make the program very concise. However, if you are using a lambda expression that contains too much content, or if the entity of a lambda expression is a functional function that is used in many places, it is convenient to list a function separately.

Lambda expression of c++11

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.