Review C ++ lambda and pythonlambda from python

Source: Internet
Author: User

Review C ++ lambda and pythonlambda from python

Lambda is an anonymous function. python lambda can make simple expressions of simple functions. C ++ lambda can implement functions similar to nested functions.

Python lambda

Lambda [arg1 [, arg2, arg3 .... argN]: expression # def defined function def foo (): return 'Hello world' print (foo () # print (lambda: 'Hello World') # because it is not saved to a variable, it becomes invalid after use, improving the performance of foo_1 = lambda: 'Hello world' print (foo_1 ()) # Add the parameter foo_3 = lambda a, B = 10: a + bprint (foo_3 (20, 10) foo_4 = lambda * args: argsprint (foo_4 )) # lambda expressions must be in one line. If a python expression cannot be written in one line, use \ newline foo_5 = lambda ** kwargs: \ kwargsprint (foo_5 (a = 1, B = 2, c = 3 )) # You can also assign values between variables: foo_2 = foo_1print (type (foo_2 ))

Lambda of VC ++ 14

Lambda is exclusive to vc ++. After vc ++ 11, this function is extended mainly to make code writing concise. gcc does not have this function.

 

Directly used. Some functions that are not reused are automatically discarded after use.

int result = [](int a) {return a*a; }(10);cout << result << endl;

You can use variables to store them repeatedly.

auto pow_2 = [](int a) {return a*a; };cout << pow_2(10) << endl;

Lambda type

Cout <typeid (pow_2). name () <endl; // class <lambda_a10bdd2a3443eccb15c2cfee0f251b1b>

 

  1. Capture clause(Also known as the C ++ SpecificationLambda Guide.)

  2. Parameter List(Optional ). (Also knownLambda declarative)

  3. Variable Specification(Optional ).

  4. Exception Specification(Optional ).

  5. Trailing return type(Optional ).

  6. "Lambda"

1. lambda Guide

The location can be passed in to external parameters, and external data can be directly transferred through value or address transfer.

[&] Pass in all external variables by reference (The address will not change), [=] Pass in all external variables by numerical value, [I] specify the variable to pass the value, [& I] specify the variable address

Int I = 10; int front = (int) & I; // front is the address of I cout <I <"-->" <front <endl; // output the current address cout <& front <endl; // view the front Address auto func_1 = [I, & front] {// pass in the I (pass the value) and front (Transfer address) int back = (int) & I; cout <I <"-->" <back </output address "\ nback-front =" <abs (back-front) <endl; cout <& front <endl; // view the front address again}; func_1 (); // obtain/* 10 --> 7601564 first i0073FD90 first front10 --> 7601536 second iback-front = 280073FD90 second front */

 

 

Struct S {void f (int I) ;}; void S: f (int I) {[&, I] {}; // when all are references, you can specify a parameter not to reference [&, & I] {}; // when all references are made by default, you cannot specify I as reference [=, this] {}; // ERROR: this when = is the default [I, I] {}; // the variable cannot be repeated}

 

 

2. The parameter list. auto can use a generic value or specify a type.

char* a = "aaa";char* b = "bbb";[](auto& a, auto& b) {    auto temp = a;    a = b;    b = temp;}(a,b);cout << a <<" "<< b << endl;

 

3. Variable Specification

Generally, variables obtained by passing values cannot be modified.

Int n = 0; [&, n] () {+ n ;}(); // error message: "n": cannot be modified in immutable lambda captured by copying

 

Mutable allows lambda to modify the value of n. Since n is captured by the value, the value of n does not change after lambda ends.

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

 

4. Exception Specification

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

char a[10] = "aaa";char* b = "bbb";auto merge_str=[](char* a, char* b)->string{    char* result = strcat(a, b);    return result;};cout << merge_str(a, b)<<"-->"<<typeid(merge_str(a, b)).name() << endl;

 

6. lambda body, which can be placed into any code block

 

Lambda expression Assignment Method:

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

 

Find_if () parameter 1: iterator header parameter 2: iterator tail parameter 3, returns a pointer return value: pointer to the element found

vector<char*>v1 = { "aaa","bbb","ccc","ddd","eee" };auto result=find_if(v1.begin(), v1.end(), [](char* n) { return n=="ccc"; });cout << *result << endl;
ccc

 

Lambda nesting

int result = [](int x) {return [&x](int y) {return (x + y); }(10); }(20);cout << result << endl;
30

 

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.