The Lambda expression and for_each syntax added to the C ++ 11 standard change the meaning of the auto keyword.

Source: Internet
Author: User

C ++ 11New StandardLambdaExpression,For_eachSyntax, and changedAutoThe meaning of the keyword.

LambdaThe expression is an anonymous function, and the entire function body is directly embedded in a commonCode.

For_eachYesC ++ 11StandardSTLThe newly added function template in the library is declared in<Algorithm>Header file.

AutoOriginal keywordCThe meaning in the language is automatic type. The currentC ++ 11New StandardsAutoThe keyword is changed to any type, but it is not a weak type and is still a strong type.AutoThe variables declared by the keyword must be initialized. The type is determined during initialization, that is, the type of the returned value of the initialized expression. Example codeAutoN = 1;, ThenNIs specifiedIntType.

Sort code:

 /*  * Test. CPP-Lambda expression and for_each test ** C ++ 11 standard Lambda expression. for_each function template ** copyright ye Jianfei 2012 *** compilation command: * g ++ test. CPP-O test-STD = C ++ 0x-wall  */ # Include <Iostream> # Include <Cstdlib> # Include <Algorithm> //  Sort function template and for_each function Template # Include <functional> //  Function Template  Using   Namespace  STD;# Ifndef_ Countof  # Define _ Countof (_ array) (sizeof (_ array)/sizeof (_ array [0]) # Endif Int  Main (){  Int A [] = { 1 , 2 , 5 , 9 , 52 , 6 , 3 , 14  }; Function < Bool ( Const   Int &, Const   Int &)> Compare;AutoOutput = [] ( Int N)-> Void {Cout <n < Endl ;};  //  In the C ++ 11 standard, the auto keyword is of any type and the type is determined by the initialization expression.  //  "[] (Int n)-> void {cout <n <Endl;}" is a Lambda expression  Cout < " Sort in ascending order  " < Endl; compare = [] ( Const   Int &, Const   Int & B)-> Bool { Return A < B ;}; sort (a, + _ Countof (A), compare); for_each (a, + _ Countof (A), output); cout <Endl; cout < "  Sort in descending order  " < Endl; compare = [] ( Const   Int &, Const   Int & B)-> Bool { Return B < A ;}; sort (a, + _ Countof (A), compare); for_each (a, +_ Countof (A), output); cout < Endl;  Return  Exit_success ;} 
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.