c ++ new features --- lambda expression

#include <algorithm> 
#include <the iostream> 
#include <Functional> 
#include <Vector> 
#include <numeric> 
#include <Array> 
#include <CString> 
#include <cstdio>
 the using  namespace   STD;
 // solution nested functions problems 
int MAIN1 () 
{ 
    // [] {COUT << "Hello lambda";} (); // anonymous lambda expressions, () calls the role played last bracket 

    Auto Fun = [] {COUT << " Hello the lambda " ;}; // function pointer 
    Fun ();// call 

} 
intMAIN2 () 
{ 
    // {} function body, execution
     // () parameter list, the member function {}, () call
     // [] (char * STR) STR {COUT << << endl;} ( "huahaua" ); 
    Auto Fun = [] ( char * STR) STR {COUT << << endl;}; 
    Fun ( " Fangfang " ); 
    COUT << the typeid (Fun) .name () << endl; // nature of the class package 
    COUT << ( void *) Fun << endl; // the lambda can not directly take the address, not as a function pointer 

} 
int main3 () 
{ 
    Auto Fun = [] ( Double a,double b){return a + b; };
    COUT << Fun ( 10 , 19.1 ) << endl;
     // -> between () {} return type specified 
    Auto fun1 = [] ( Double A, Double B) -> int { return A + B; }; 
    COUT << fun1 ( 10 , 19.1 ) << endl;
     // inline-expansive, not fetch address 


    // -> the decltype (A + B) type inference 
    Auto fun2 = [] ( Double A, Double B) -> the decltype (A + B) { return A + B;}; 
    COUT << fun2 ( 10 , 19.1) << endl; 

} 
int main () 
{ 

    int NUM = 100 ; 
    Auto Fun = [] ( int NUM) = {NUM . 5 ; NUM COUT << << endl;}; 
    Fun (NUM); // follow-copy , copy 
    COUT << " main: " << NUM << endl; 
}

 

Example 2:

#include <algorithm> 
#include <the iostream> 
#include <Functional> 
#include <Vector> 
#include <numeric> 
#include <Array> 
#include <CString> 
#include <cstdio>
 the using  namespace   STD;
 // the lambda expression is primarily solve embedded code, CPP supports lambda expressions
 // [] () multable-> int {} (); // anonymous expression
 // [] = references can only read, can not be changed = mutable read a copy of the original reform , & read original
 // () parameters, int A, B int
 // {statement} achieved, () call
 // -> specify the return value
 // C11 does not support auto.c14 support 
int MAIN1 () 
{ 
    int= num1 100 , num2 = 99 ;
     // = external variables can only be read, not written
     // [=] () {= 20 is num1, num2 = 30; COUT << << num1 num2 << endl;} () ; 
    
    // & writable external variable 
    [&] () = {num1 20 is , num2 = 30 ; COUT << << num1 num2 << endl;} ();     
    
    // = plus mutable, as a copy for the external variables modified, without affecting the external variables 
    [=] () {num1 the mutable = 20 is , num2 = 30 ; COUT << << num1 num2 << endl;} (); 
    COUT << " main: " << << num1 "    " << num2 << endl;
    system("PAUSE " ); 
    
} 
int MAIN2 () 
{ 

    int A = 10 , B = . 9 , C = . 8 ;
     // & A, readable and writable, b, c can only read
     // [& A, b, c] () { = 111 A, COUT << << A B C << << endl;} ();
     // A, B, C can only be read
     // [A, B, C] () {A = 111, COUT < <a << B C << << endl;} ()
     // the mutable copy, can read and write, the original is read, to write a copy of 
    [a, B, C] () {the mutable a = 111 , B = 123 , C = 321 , COUT << << A B C << << endl;} ();
    // [=] can specify all 

} 
int main3 () 
{
    // only applies to vs2015 
    [] (auto a, auto b) A + B {COUT << << endl;} ( 10 , . 11 ); // C ++ data types reasoning 14 
    [] (auto a, auto b ) {A + B COUT << << endl;} ( 10.8 , . 11 ); 
    [] (Auto A = 0 , Auto B = 0 ) {A + B COUT << << endl;} ( 10.8 , . 11 ); 
    [] ( int A = 0 , int B = 0 ) {A + B COUT << << endl;} ( 10.8 , . 11 ); 

} 
int main () 
{ 
    Array <int, 10> myint{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };//cpp风格数值
    for_each(myint.begin(), myint.end(), [](int num){cout << num << " "; });//显示数值
    for_each(myint.begin(), myint.end(), [](int& num){num + 1, cout << num << " "; });//修改
    for_each(myint.begin(), myint.end(), [](intNUM) {NUM COUT << << "  " ;}); // display the modified value 
}

 

Guess you like

Origin www.cnblogs.com/bwbfight/p/11299100.html