美文网首页
仿函数——Functor

仿函数——Functor

作者: XavierQu | 来源:发表于2014-04-06 22:00 被阅读342次

简单的需求

比如,有一个简单需求:找到一个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--

相关文章

  • 仿函数——Functor

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

  • Functor --- 仿函数

    仿函数(functors)在C++标准中采用的名称是函数对象(function objects),实际上就是函数对...

  • STL ----仿函数

    C++STL学习(9)仿函数(function objects, functor) - CSDN博客 List ,...

  • 仿函数/函数对象 functor / function obje

    1 仿函数 对某个 class 重载 函数调用操作符 operator(), 它就成为 Functor 可 像 f...

  • C++ 仿函数

    仿函数 定义:仿函数(functor),就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator...

  • C++仿函数(functor)

    仿函数functor,就是使一个类的使用看上去象一个函数。其实现就是类中实现一个operator,这个类就有了类似...

  • functor相等性判断

    侯杰将cpp中的functor翻译成仿函数,它其实是个class或者struct,通过匿名调用operator()...

  • 从bind2nd函数看懂C++ STL的适配器与仿函数

      适配器adapter与仿函数functor是C++ 标准库中提供的部件,可以将STL提供的一些基本算法(比如s...

  • 函数对象/functor 1 Base classes (1) unary_functi...

  • 第十六章 string类和标准模板库(5)函数对象

    (五)函数对象 函数对象,也叫作函数符functor。函数符是可以以函数的方式与()结合的任意对象,包括函数名,函...

网友评论

      本文标题:仿函数——Functor

      本文链接:https://www.haomeiwen.com/subject/hpkltttx.html