仿函数——Functor

2014-04-06  本文已影响342人  XavierQu

简单的需求

比如,有一个简单需求:找到一个vector<string>中,长度小于3的字符串的数目。解决方法可能会是:

int count(const std::vector<std::string>& str_vec, const size_t threshold)
{   
    int size = 0;
    std::vector<std::string>::const_iterator it; 
    for (it = str_vec.begin(); it != str_vec.end(); ++ it) {
        if (it->length() < threshold) {
            ++ size;
        }   
    }   
    return size;
}   

其实,数据STL的同学应该知道有个count_if函数。count_if的功能就是对于某种容器,对符合条件的元素进行计数。count_if包含三个参数,容器的开始地址、容器的结束地>
址、以及参数为元素类型的函数。

使用count_if的代码可以这样写:

bool test(const std::string& str) { return str.length() < 3; }
int count(const std::vector<std::string>& str_vec)
{   
    return std::count_if(str_vec.begin(), str_vec.end(), test);
}   

但是,这样有个问题:没有扩展性。比如,判断的字符串由长度3变成5呢?将test函数上面再增加一个长度参数可以吗?不行,count_if的实现就决定了test必须是单一参数的。既想满足count_if的语法要求,又需要让判断的函数具有可扩展性,这时候就需要functor了。

functor登场

functor的含义是:调用它就像调用一个普通的函数一样,不过它的本质是一个类的实例的成员函数(operator()这个函数),所以functor也叫function object
因此以下代码的最后两个语句是等价的:

class SomeFunctor
{
public:
    void operator() (const string& str)
    {
        cout << "Hello " << str << end;
    }
};

SomeFunctor functor;
functor("world");               //Hello world
functor.operator()("world");    //Hello world

其实,它并不算是STL中的一部分,不过需要STL中的函数都把functor所谓参数之一,functor起到了定制化的作用。functor与其它普通的函数相比,有一个明显的特点:**
可以使用成员变量**。这样,就提供了扩展性。

继续上面例子,写成functor的形式:

class LessThan
{
public:
    LessThan(size_t threshold): _threshold(threshold) {}
    bool operator() (const std::string str) { return str.length() < _threshold; }
private:
    const size_t _threshold;
};

int count(const std::vector<std::string>& str_vec)
{
    LessThan less_than_three(3);
    return std::count_if(str_vec.begin(), str_vec.end(), less_than_three);
    //LessThan less_than_five(5);
    //std::count_if(str_vec.begin(), str_vec.end(), less_than_five);
}

int count_v2(const std::vector<std::string>& str_vec, const size_t threshold)
{
    return std::count_if(str_vec.begin(), str_vec.end(), LessThan(threshold));
}

C++11的新玩法

有人可能会说,我已经有了自己实现的判断函数了,但是直接用又不行,有啥解决办法吗?
其实是有的!(我也是最近才发现的)

C++11的标准中,提供了一套函数,能将一个普通的、不符合使用方要求的函数,转变成一个符合参数列表要求的functor,这实在是太酷了!

比如用户自己实现的int test(const std::string& str_vec, const size_t threshold)函数,如果能将第二个参数进行绑定,不就符合count_if的要求了吗?

新标准的C++就提供了这样一个函数——bind

通过std::bind以及std::placeholders,就可以实现转化,样例代码如下:

bool less_than_func(const std::string& str, const size_t threshold)
{
        return str.length() < threshold;
}

//提供 _1 占位符
using namespace std::placeholders;
//绑定less_than_func第二个参数为5, 转化为functor
auto less_than_functor = std::bind(less_than_func, _1, 5);
std::cout << std::count_if(str_vec.begin(), str_vec.end(), less_than_functor) << std::endl;

参考资料

--EOF--

上一篇 下一篇

猜你喜欢

热点阅读