声明:本文参考了Alex Allain的文章http://www.cprogramming.com/c++11/c++11-lambda-closures.html
加入了自己的理解,不是简单的翻译
C++11终于知道要在语言中加入匿名函数了。匿名函数在很多时候可以为编码提供便利,这在下文会提到。很多语言中的匿名函数,如C++,都是用Lambda表达式实现的。Lambda表达式又称为lambda函数。我在下文中称之为Lambda函数。
为了明白Lambda函数的用处,请务必先搞明白C++中的自动类型推断:http://blog.csdn.net/srzhz/article/details/7934483
基本的Lambda函数
#include <iostream> using namespace std; int main() { auto func = [] () { cout << "Hello world"; }; func(); // now call the function }
其中func就是一个lambda函数。我们使用auto来自动获取func的类型,这个非常重要。定义好lambda函数之后,就可以当这场函数来使用了。
[] () -> int { return 1; }
[captures] (params) -> ret {Statments;}
Lambda函数的用处
#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函数提供的参数是Func类型,这是一个泛型类型。在使用过程中应该传入一个函数,然后分别对地址簿中每一个entry执行这个函数,如果返回值为真那么表明这个entry符合使用者的筛选要求,那么就应该放入结果当中。那么这个Func类型的参数如何传入呢?
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; } ); }
可以看到,我们在调用函数的时候直接定义了一个lambda函数。参数类型是
const string& addr
Lambda函数中的变量截取
// read in the name from a user, which we want to search string name; cin>> name; return global_address_book.findMatchingAddresses( // notice that the lambda function uses the the variable 'name' [&] (const string& addr) { return name.find( addr ) != string::npos; } );
从上述代码看出,我们的lambda函数已经能使用外部作用域中的变量name了。这个lambda函数一个最大的区别是[]中间加入了&符号。这就告诉了编译器,要进行变量截取。这样lambda函数体就可以使用外部变量。如果不加入任何符号,编译器就不会进行变量截取。
- [] 不截取任何变量
- [&} 截取外部作用域中所有变量,并作为引用在函数体中使用
- [=] 截取外部作用域中所有变量,并拷贝一份在函数体中使用
- [=, &foo] 截取外部作用域中所有变量,并拷贝一份在函数体中使用,但是对foo变量使用引用
- [bar] 截取bar变量并且拷贝一份在函数体重使用,同时不截取其他变量
- [this] 截取当前类中的this指针。如果已经使用了&或者=就默认添加此选项。
Lambda函数和STL
vector<int> v; v.push_back( 1 ); v.push_back( 2 ); //... for ( auto itr = v.begin(), end = v.end(); itr != end; itr++ ) { cout << *itr; }
现在有了lambda函数你就可以这么写
vector<int> v; v.push_back( 1 ); v.push_back( 2 ); //... for_each( v.begin(), v.end(), [] (int val) { cout << val; } );
而且这么写了之后执行效率反而提高了。因为编译器有可能使用”循环展开“来加速执行过程(计算机系统结构课程中学的)。
http://www.nwcpp.org/images/stories/lambda.pdf 这个PPT详细介绍了如何使用lambda表达式和STL