分享

CPP仿函数基本概念浅析

 fisher60 2012-09-01

C++仿函数到底是什么呢?其实它就是一个能像函数一样工作的东西,它的主要作用将会在这篇文章中进行详细的介绍。

C++编程语言为我们带来了非常大的好处。不过即使是我们经常使用的功能也有很多比较高深的内容值得我们去深入的探讨。在这里我们将会为大家详细介绍一下C++仿函数的基本概念,方便大家对这一语言的解读。

C++仿函数这个词经常会出现在模板库里(比如 STL),那么什么是仿函数呢?

顾名思义:仿函数就是能像函数一样工作的东西,请原谅我用东西这样一个代词,下面我会慢慢解释。

  1. void dosome( int i )

这个 dosome 是一个函数,我们可以这样来使用它: dosome(5);

那么,有什么东西可以像这样工作么?

答案1:重载了 () 操作符的对象,比如:

  1. struct DoSome
  2. {
  3. void operator()( int i );
  4. }
  5. DoSome dosome;

这里类(对 C++ 来说,struct 和类是相同的) 重载了 () 操作符,因此它的实例 dosome 可以这样用 dosome(5); 和上面的函数调用一模一样,不是么?所以 dosome 就是一个C++仿函数了。

实际上还有答案2:

函数指针指向的对象。

  1. typedef void( *DoSomePtr )( int );
  2. typedef void( DoSome )( int );
  3. DoSomePtr *ptr=&func;
  4. DoSome& dosome=*ptr;
  5. dosome(5); // 这里又和函数调用一模一样了。

当然,答案3 成员函数指针指向的成员函数就是意料之中的答案了。

C++仿函数的用处

不管是对象还是函数指针等等,它们都是可以被作为参数传递,或者被作为变量保存的。因此我们就可以把一个仿函数传递给一个函数,由这个函数根据需要来调用这个仿函数(有点类似回调)。

STL 模板库中,大量使用了这种技巧,来实现库的“灵活”。 比如: for_each, 它的源代码大致如下:

  1. template < typename Iterator, typename Functor >
  2. void for_each( Iterator begin, Iterator end, Fucntor func )
  3. {
  4. for( ; begin!=end; begin++ )
  5. func( *begin );
  6. }

这个 for 循环遍历了容器中的每一个元素,对每个元素调用了仿函数 func,这样就实现了 对“每个元素做同样的事”这样一种编程的思想。 特别的,如果仿函数是一个对象,这个对象是可以有成员变量的,这就让C++仿函数有了“状态”,从而实现了更高的灵活性。

 

 

template function

for_each

<algorithm>
template <class InputIterator, class Function>
   Function for_each (InputIterator first, InputIterator last, Function f);
Apply function to range

Applies function f to each of the elements in the range [first,last).

The behavior of this template function is equivalent to:

1
2
3
4
5
6
template<class InputIterator, class Function>
  Function for_each(InputIterator first, InputIterator last, Function f)
  {
    for ( ; first!=last; ++first ) f(*first);
    return f;
  }




 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// for_each example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void myfunction (int i) {
  cout << " " << i;
}

struct myclass {
  void operator() (int i) {cout << " " << i;}
} myobject;

int main () {
  vector<int> myvector;
  myvector.push_back(10);
  myvector.push_back(20);
  myvector.push_back(30);

  cout << "myvector contains:";
  for_each (myvector.begin(), myvector.end(), myfunction);

  // or:
  cout << "\nmyvector contains:";
  for_each (myvector.begin(), myvector.end(), myobject);

  cout << endl;

  return 0;
}



Output:

myvector contains: 10 20 30
myvector contains: 10 20 30


 

Complexity

Linear: Applies f once to each element.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多