好比,有一个简单需求:找到一个vector<string>
中,长度小于3的字符串的数目。解决方法可能会是:less
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
的代码能够这样写:spa
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
了。code
functor
登场functor
的含义是:调用它就像调用一个普通的函数同样,不过它的本质是一个类的实例的成员函数(operator()
这个函数),因此functor
也叫function object
。
所以如下代码的最后两个语句是等价的:three
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
的形式:get
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)); }
有人可能会说,我已经有了本身实现的判断函数了,可是直接用又不行,有啥解决办法吗?
实际上是有的!(我也是最近才发现的)string
C++11的标准中,提供了一套函数,能将一个普通的、不符合使用方要求的函数,转变成一个符合参数列表要求的functor
,这实在是太酷了!it
好比用户本身实现的int test(const std::string& str_vec, const size_t threshold)
函数,若是能将第二个参数进行绑定,不就符合count_if
的要求了吗?io
新标准的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--