C++11-----everything from lambda.

Source: Internet
Author: User
Tags readable

Lambda expression

Read Meng Yan a big blog (http://blog.csdn.net/myan/article/details/5877305), I decided to learn some main line content:
①LAMBDA-expression
②function/bind
③ rvalue Reference

This article records the application of lambda expressions: why Lambda:

Lambda expressions have actually been touched before, but have been used rarely by VS2005. Some big said: Do not learn lambda expression, do not want to read Stl,boost,ace source code. I do not know is true, in addition to the STL small even boost has not been used, ace in addition to the university when the egg pain configured an environment, and then typed in the first example of the book output log, and then there is no then.
There is no nested definition of a function in C + +, and the lambda expression compensates for this flaw. With some small functions, implemented by lambda expressions, it increases the tightness of the code, facilitates comprehension and reading (Will the execution be more efficient?). )。

What LAMBDA:

[=,&ref,val] (int a,int b) mutable->double{statement;};
①=:LAMBDA can access variables outside, but not writable
②&REF: Referencing external variables, readable writable can be returned (reference pass)
③val: Indicates value passing, readable and not writable
④ (int A, int b) it doesn't explain.
⑤mutable: Indicates that a non-referenced value is also readable and writable, but cannot be returned
⑥->double Determining the return value type inside a block
⑦{statement;} function block
⑧: Take out the contents of the function pointer to the address

These are from Wikipedia:
[]//does not define any of the variables. Using undefined variables will cause mistakes.
[x, &y]//x is transferred by means of a value (preset) and y is transferred by reference.
[&]//Any external variables used are implicitly referenced by reference.
[=]//Any external variables that are used are implicitly referenced in a value-based manner.
[& x]//x is referenced by a value. The remainder is quoted in the reference form.
[=, &z]//Z is referenced in a reference way. The remainder is quoted in the form of a value.

How LAMBDA:
#include <iostream>#include <vector>#include <algorithm>#include <functional>using namespace STD;//LAMBDA FoundationvoidLambda_one ();Lambda instances in//sortvoidLambda_two ();//lambda function RecursionvoidLambda_three ();intMain () {lambda_one (); Lambda_two (); Lambda_three (); GetChar ();return 0;}voidLambda_one () {cout<<endl <<"------------Lambda---------------------without a function parameter"<<endl;AutoFun = [] () {};AutoFUN1 = [] () {cout<<"Fun1"<<endl;}; Fun1 ();cout<<endl <<"------------For_each using a simple lambda----------------"<<endl;STD:: vector<int>V3,5); For_each (V.begin (), V.end (), [] (intNUM) {cout<< Num <<"\ T";});cout<<endl <<"------------Set the return value type of the lambda--------------------"<<endl;cout<<[] (DoubleADoubleb) {returnA + b;} (1.4,2.5) <<endl;cout<<[] (DoubleADoubleb)int{returnA + b;} (1.4,2.5) <<endl;cout<<endl <<"------------a value----------------------------in lambda"<<endl;intx =Ten;inty = -; [=] (DoubleADoublebmutable-int{cout<<"Lambda:"<< (x = -) <<"\ T"<< (y =Ten) <<endl;returnA + b; }(1.4,2.5);cout<<"Main:"<< x <<"\ T"<< y <<endl;cout<<endl <<"------------a reference--------------------------in lambda"<<endl; [&x,&y] (DoubleADoublebmutable-int{cout<<"Lambda:"<< (x = -) <<"\ T"<< (y =Ten) <<endl;returnA + b; }(1.4,2.5);cout<<"Main:"<< x <<"\ T"<< y <<endl;cout<<endl <<"------------Pass-through reference and reference in Lambda------------------"<<endl;the//equals sign must be written in front, or [x,&y].    //= says that all other external variables, except &y, can be accessed by value passing. [=,&y] (DoubleADoublebmutable-int{cout<<"Lambda:"<< (x = -) <<"\ T"<< (y =Ten) <<endl;returnA + b; }(1.4,2.5);cout<<"Main:"<< x <<"\ T"<< y <<endl;}voidLambda_two () {//1.sort Sort    cout<<endl<<"Use lambda------------------------in------------sort Sort"<<endl;inta[8] = {6,8,3,4,9,2,7,1}; Sort (Begin (a), end (a), [] (Const int&a,Const int&AMP;B)BOOL{returna < b;}); For_each (Begin (a), end (a), [] (Const int&num) {cout<< Num <<"\ T";});cout<<endl<<"---------------------------------------------------------"<<endl;}voidLambda_three () {//2. (lambda recursion) 3 numbers return the maximum of two numbers and    cout<<endl<<"------------Lambda recursive-----------------------------------"<<endl; function<int(int,int,int) > f = [&f] (intAintBintc)int{if(a<= b && a <= c) {returnB + C; }returnf (b,c,a); };cout<< F (4,5,6) <<endl;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C++11-----everything from lambda.

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.