C++仿函数这个词经常会出现在模板库里(比如 STL),那么什么是仿函数呢?
顾名思义:仿函数就是能像函数一样工作的东西,请原谅我用东西这样一个代词,下面我会慢慢解释。
- void dosome( int i )
这个 dosome 是一个函数,我们可以这样来使用它: dosome(5);
那么,有什么东西可以像这样工作么?
答案1:重载了 () 操作符的对象,比如:
- struct DoSome
- {
- void operator()( int i );
- }
- DoSome dosome;
这里类(对 C++ 来说,struct 和类是相同的) 重载了 () 操作符,因此它的实例 dosome 可以这样用 dosome(5); 和上面的函数调用一模一样,不是么?所以 dosome 就是一个C++仿函数了。
实际上还有答案2:
函数指针指向的对象。
- typedef void( *DoSomePtr )( int );
- typedef void( DoSome )( int );
- DoSomePtr *ptr=&func;
- DoSome& dosome=*ptr;
- dosome(5); // 这里又和函数调用一模一样了。
当然,答案3 成员函数指针指向的成员函数就是意料之中的答案了。
C++仿函数的用处
不管是对象还是函数指针等等,它们都是可以被作为参数传递,或者被作为变量保存的。因此我们就可以把一个仿函数传递给一个函数,由这个函数根据需要来调用这个仿函数(有点类似回调)。
STL 模板库中,大量使用了这种技巧,来实现库的“灵活”。 比如: for_each, 它的源代码大致如下:
- template < typename Iterator, typename Functor >
- void for_each( Iterator begin, Iterator end, Fucntor func )
- {
- for( ; begin!=end; begin++ )
- func( *begin );
- }
这个 for 循环遍历了容器中的每一个元素,对每个元素调用了仿函数 func,这样就实现了 对“每个元素做同样的事”这样一种编程的思想。 特别的,如果仿函数是一个对象,这个对象是可以有成员变量的,这就让C++仿函数有了“状态”,从而实现了更高的灵活性。
for_each
template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function f);
Applies function f to each of the elements in the range [first,last).
The behavior of this template function is equivalent to:
|
|
Parameters
- first, last
- Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
- f
- Unary function taking an element in the range as argument. This can either be a pointer to a function or an object whose class overloads operator().
Its return value, if any, is ignored.
Return value
The same as f.
Example
|
|
Output:
myvector contains: 10 20 30 myvector contains: 10 20 30 |
Complexity
Linear: Applies f once to each element.