Review of C + + lambda by Python for Ingress

Source: Internet
Author: User

Lambda is an anonymous function, Python lambda can make simple functions concise expression, C + + lambda makes the function similar to nested functions are implemented

Python's lambda

Lambda[Arg1[,arg2,arg3....argn]]:expression#a function defined with Defdeffoo ():return 'Hello World'Print(foo ())#functions defined with lambdaPrint(Lambda:'Hello World')#because it is not saved to a variable, it is invalidated after it is exhausted and improves the performancefoo_1=Lambda:'Hello World'Print(Foo_1 ())#Add parameterfoo_3=Lambdaa,b=10:a+bPrint(Foo_3 (20,10)) Foo_4=Lambda*Args:argsPrint(Foo_4 (1,2,3,4))#The lambda expression must be on one line, and the Python line cannot be wrapped with \foo_5=Lambda**Kwargs:kwargsPrint(Foo_5 (a=1,b=2,c=3))#You can also assign values between variablesFoo_2=Foo_1Print(Type (foo_2))

Vc++14 's Lambda

Lambda is unique to VC + +, after vc++11, the expansion of this function is mainly to make the code concise, GCC does not have this function

Direct use, for some non-reusable functions, the use of automatic discard

int result = [] (int a) {return a*a;} (<< result << Endl;

If you want to reuse it, you can use variables to store

Auto Pow_2 = [] (int a) {return a* << pow_2 (ten) << Endl;

Types of Lambda

cout << typeid (pow_2). Name () << Endl;      // type is class <lambda_a10bdd2a3443eccb15c2cfee0f251b1b>

  1. The Capture clause , also known as a Lambda bootstrap in the C + + specification. )

  2. parameter list (optional). (also known as a lambda declarator )

  3. variable specification (optional).

  4. Exception Specification (optional).

  5. trailing return type (optional).

  6. "Lambda Body"

1,LAMBDA Boot

Position can be passed in external parameters, can pass the value or the way to pass directly to the external data

[&] passing in all external variables by reference (address does not change), [=] passing values to all external variables, [i] specifying variable values, [&i] specifying variable address

inti =Ten;intFront = (int) &i;//Front is the address of Icout << i <<" -"<< Front << Endl;//output this address .cout << &front << Endl;//View Front's addressAuto Func_1 = [I,&front] {//Incoming I (pass value) and front (address)    intBack = (int) &i; cout<< I <<" -"<< back <<//output this address .        "\nback-front="<< ABS (Back-front) <<Endl; cout<< &front << Endl;//View front address again};func_1 ();//come to/*10-->7601564 first time I0073fd90 first time front10-->7601536 second time iback-front=280073fd90 second time fr Ont*/

struct void f (int  i);}; void S::f (int  i) {    [&, i]{};    // when all are references, you can specify a non-reference    [&, &i] {};   // when the default is all referenced, you cannot specify I as the reference     This // Error:this when = is the default    [I, I] {};    // variable cannot be duplicated }

2, argument list, auto using generic value, or specifying type

Char " AAA " ; Char " BBB " ; [] (Auto& A, auto& b) {    = A;     = B;     =<< a <<""<< b << Endl;

3, variable specification

In general, variables obtained by passing values cannot be modified

int 0 ; [&, n] () {+ +N;} (); // hint Error: "n": Could not be modified in non-mutable lambda by copy capture

The mutable specification allows you to modify the value of N in a lambda, since n is captured by value, and the value of n does not change after the lambda ends

int 0 ; int 0 ; [&, n] (int a) mutable {m = ++n + A;} (4"" << n << Endl;
5
0

4, exception specification

5, the return value is changed from char* to string

Chara[Ten] ="AAA";Char* B ="BBB"; auto Merge_str=[](CharAChar* b)string{    Char* result =strcat (A, b); returnresult;}; cout<< Merge_str (A, b) <<" -"<<typeid (Merge_str (A, b)). Name () << Endl;

6,lambda body, can be put into arbitrary code block

Lambda expression Assignment Method:

// 1, can be assigned to auto variable auto f1 = [] (intint y) {return x +<< F1 (1,  2) << Endl; // 2, can be assigned to the function object function<int(intint) >F2 = [] (intint y) {return x +<< f2 (3,4) << Endl;
3
7

FIND_IF () parameter one: iterator head parameter two: iterator tail parameter Three, returns a pointer return value: Pointer to the element found

vector<Char*&GT;V1 = {"AAA","BBB","CCC","DDD","Eee"};auto Result=find_if (V1.begin (), V1.end (), [] (Char* N) {returnn=="CCC"; }); cout<< *result << Endl;
Ccc

Lambda nesting

int result = [] (int x) {return [&x] (int y) {return (x + y);} (ten); } (<< result << Endl;
30

Review of C + + lambda by Python for Ingress

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.