C++11新特性:Lambda函数(匿名函数)


C++11终于知道要在语言中加入匿名函数了。匿名函数在很多时候可以为编码提供便利,这在下文会提到。很多语言中的匿名函数,如C++,都是用Lambda表达式实现的。Lambda表达式又称为lambda函数。我在下文中称之为Lambda函数。

为了明白Lambda函数的用处,请务必先搞明白C++中的自动类型推断:

基本的Lambda函数

我们可以这样定义一个Lambda函数:

#include <iostream>

using namespace std;

int main()
{
    auto func = [] () { cout << "Hello world"; };
    func(); // now call the function
}

其中func就是一个lambda函数。我们使用auto来自动获取func的类型,这个非常重要。定义好lambda函数之后,就可以当这场函数来使用了。

其中 [ ] 表示接下来开始定义lambda函数,中括号中间有可能还会填参数,这在后面介绍。之后的()填写的是lambda函数的参数列表{}中间就是函数体了。

正常情况下,只要函数体中所有return都是同一个类型的话,编译器就会自行判断函数的返回类型。也可以显示地指定lambda函数的返回类型。这个需要用到函数返回值后置的功能,比如这个例子:

[] () -> int { return 1; }

所以总的来说lambda函数的形式就是:

[captures] (params) -> ret {Statments;}

Lambda函数的用处

假设你设计了一个地址簿的类。现在你要提供函数查询这个地址簿,可能根据姓名查询,可能根据地址查询,还有可能两者结合。要是你为这些情况都写个函数,那么你一定就跪了。所以你应该提供一个接口,能方便地让用户自定义自己的查询方式。在这里可以使用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

返回值是bool类型。

如果用户要使用不同的方式查询的话,只要定义不同的lambda函数就可以了。

  • 1
  • 2
  • 下一页

相关内容