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 ;}