參考文獻:C++11新特性:Lambda函數(shù)(匿名函數(shù))
匿名函數(shù)不翩,C++11里面加入了匿名函數(shù)。
1 定義一個基本的Lambda匿名函數(shù)
#include <iostream>
using namespace std;
int main()
{
auto func = [] () { cout << "Hello world"; };
func(); // now call the function
}
func就是定義的匿名函數(shù)麻裳,使用auto自動獲取func的類型口蝠,定義好之后,就可以當做常函數(shù)使用了津坑。
[]表示接下來定義lambda函數(shù)妙蔗,中括號中間有可能還會填參數(shù),之后的()填寫的是lambda函數(shù)的參數(shù)列表国瓮,{}中間是函數(shù)體灭必。
正常情況下狞谱,只要函數(shù)體中所有return都是同一個類型的話乃摹,編譯器就會自行判斷函數(shù)的返回類型。也可以顯示地指定lambda函數(shù)的返回類型跟衅。這個叫函數(shù)返回值后置的功能:
[] () -> int { return 1; }
總的來說孵睬,lambda函數(shù)的形式是:
[captures] (params) -> ret {Statments;}
2 Lambda函數(shù)的用處
假設你設計了一個地址簿的類。現(xiàn)在你要提供函數(shù)查詢這個地址簿伶跷,可能根據(jù)姓名查詢掰读,可能根據(jù)地址查詢,還有可能兩者結(jié)合叭莫。要是你為這些情況都寫個函數(shù)蹈集,那么你一定就跪了。所以你應該提供一個接口雇初,能方便地讓用戶自定義自己的查詢方式拢肆。在這里可以使用lambda函數(shù)來實現(xiàn)這個功能。
#include <string>
#include <vector>
class AddressBook
{
public:
// using a template allows us to ignore the differences between functors, function pointers
// and lambda
template<typename Func>
std::vector<std::string> findMatchingAddresses (Func func)
{
std::vector<std::string> results;
for ( auto itr = _addresses.begin(), end = _addresses.end(); itr != end; ++itr )
{
// call the function passed into findMatchingAddresses and see if it matches
if ( func( *itr ) )
{
results.push_back( *itr );
}
}
return results;
}
private:
std::vector<std::string> _addresses;
};
從上面代碼可以看到,findMatchingAddressses函數(shù)提供的參數(shù)是Func類型郭怪,這是一個泛型類型支示。在使用過程中應該傳入一個函數(shù),然后分別對地址簿中每一個entry執(zhí)行這個函數(shù)鄙才,如果返回值為真那么表明這個entry符合使用者的篩選要求颂鸿,那么就應該放入結(jié)果當中。那么這個Func類型的參數(shù)如何傳入呢攒庵?
AddressBook global_address_book;
vector<string> findAddressesFromOrgs ()
{
return global_address_book.findMatchingAddresses(
// we're declaring a lambda here; the [] signals the start
[] (const string& addr) { return addr.find( ".org" ) != string::npos; }
);
}
可以看到嘴纺,我們在調(diào)用函數(shù)的時候直接定義了一個lambda函數(shù)。參數(shù)類型是 :
const string& addr
返回值是bool類型叙甸。
如果用戶要使用不同的方式查詢的話颖医,只要定義不同的lambda函數(shù)就可以了。
3 Lambda函數(shù)和STL
lambda函數(shù)的引入為STL的使用提供了極大的方便裆蒸。比如下面這個例子熔萧,當你想遍歷一個vector的時候,原來你得這么寫:
vector<int> v;
v.push_back( 1 );
v.push_back( 2 );
//...
for ( auto itr = v.begin(), end = v.end(); itr != end; itr++ )
{
cout << *itr;
}
現(xiàn)在有了lambda函數(shù)你就可以這么寫:
vector<int> v;
v.push_back( 1 );
v.push_back( 2 );
//...
for_each( v.begin(), v.end(), [] (int val)
{
cout << val;
} );
而且這么寫了之后執(zhí)行效率反而提高了僚祷。因為編譯器有可能使用”循環(huán)展開“來加速執(zhí)行過程