C 11 標準 Lambda表示式

2022-07-28 21:00:32 字數 750 閱讀 5115

c++ 11標準新增加了lambda表示式,以後小函式可以直接內嵌lambda表示式搞定了。例如排序,我們以前要這麼寫:

#include 

#include

#include

bool compare( const

int & a, const

int &b )

using

namespace

std;

intmain ( )

; sort( a, a+9

, compare );

for ( int i = 0 ; i < 9 ; i ++)

cout

<< a[i]

exit_success;

}

用c++ 11標準的lambda表示式,這麼寫就行了:

#include 

#include

#include

using

namespace

std;

intmain ( )

; sort( a, a+9, ( const

int & a, const

int & b )->bool );

for ( int i = 0 ; i < 9 ; i ++)

cout

<< a[i]

exit_success;

}

C 11 標準 Lambda表示式

c 11標準新增加了lambda表示式,以後小函式可以直接內嵌lambda表示式搞定了。例如排序,我們以前要這麼寫 include include include bool compare const int a,const int b using namespace std int main so...

c 11之lambda表示式

c 11中的lambda表示式用於定義並建立匿名的函式物件,以簡化程式設計工作。lambda的語法形式如下 函式物件引數 操作符過載函式引數 mutable或exception宣告 返回值型別 可以看到,lambda主要分為五個部分 函式物件引數 操作符過載函式引數 mutable或exceptio...

C 11之lambda表示式

lambda表示式就是匿名函式,例如 auto fun int a,int b lambda的格式如下 捕獲列表 引數 捕獲列表有下面幾種 不捕獲任何變數 捕獲外部作用域的所有變數,並作為引用在函式體內使用 按引用捕獲 捕獲外部作用域的所有變數,並作為副本在函式體內使用 按值捕獲 foo 按值捕獲外...