您的位置:首页 > 其它

STL中仿函数(functors)、类成员和mem_fun的使用

2006-04-01 10:44 429 查看
STL中仿函数(functors)、类成员和mem_fun的使用
作者: winter

众所周知,STL使用起来非常方便,其中仿函数(functor)扮演了一个非常重要的角色。灵活运用仿函数的使用对于发挥STL强大功能非常关键。本文详细介绍了如何使用mem_fun和mem_fun1来绑定类成员函数,使之成为functor

什么是仿函数?就是一个重载了"()"运算符的struct,例如:

struct print_obj{
void operator(int a)const{
cout<<a<<endl;
}
};

在STL的许多算法(algorithm)中都需要使用functor. 如:for_each. 同样在关联容器中也需要使用functor, 如map, set等。经常在使用STL算法的时候,经常需要把仿函数和类联系在一起,如果可以直接使用类的成员函数作为仿函数,那就方便多了。mem_fun的功能就是如此。

先看个简单的例子:

struct D {
D(int i=0){num=i;}
int num;
};
struct print_D{
void operator()(const D* d)const{
cout<<"I am D. my num="<<d->num<<endl;
}
};

int main()
{
vector<D*> V;

V.push_back(new D(1));
V.push_back(new D(2));
V.push_back(new D);
V.push_back(new D(3));

for_each(V.begin(), V.end(), print_D());
}
编译输出:

I am D. my num=1
I am D. my num=2
I am D. my num=0
I am D. my num=3

如果使用mem_fun,会方便很多:

struct D {
D(int i=0){num=i;}
void print() { cout << "I'm a D. my num=" << num<< endl; }
int num;
};

int main()
{
vector<D*> V;

V.push_back(new D(1));
V.push_back(new D(2));
V.push_back(new D);
V.push_back(new D(3));

for_each(V.begin(), V.end(), mem_fun(&D::print));
}

是不是省了一个仿函数?方便多了,没错吧。这也更符合面向对象的规则。不过这样好像让人难以理解,这里告诉你一个理解STL的诀窍:

如果对STL的某个部分不了解,就去看源码,源码是最好的老师。

那看看源码是怎么回事,在SGI STL的stl_function.h:

template <class _Ret, class _Tp>
inline mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)())
{ return mem_fun_t<_Ret,_Tp>(__f); }
 

原来mem_fun返回的是一个对象:mem_fun_t<_Ret,_Tp>.(不要嫌人家命名太怪异).那mem_fun_t<_Ret,_Tp>又是什么东东?还是看源码:

template <class _Ret, class _Tp>
class mem_fun_t : public unary_function<_Tp*,_Ret> {
public:
explicit mem_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
_Ret operator()(_Tp* __p) const { return (__p->*_M_f)(); }
private:
_Ret (_Tp::*_M_f)();
};

看明白了吗?原来mem_fun_t就是一个functor,这下就满足了for_each的要求了。其调用流程是这样的,for_each把vector中的元素传送给mem_fun,mem_fun自己产生一个仿函数mem_fun_t,然后仿函数调用其重载的()。过程就这么简单。当然你不能对其他类的成员函数进行绑定,因为在for_each调用过程中,会传递其*iterator值,如果是其他类的成员函数,那么这个类的对象无法传入,当然就无法完成任务了。

这里使用的是vector<D*> V; 在mem_fun_t构造函数中,刚好需要指针,如果不是D*, 而是使用vector<D> V; 还能用吗?

这是你需要使用的是mem_fun_ref。把程序改成:

struct D {
D(int i=0){num=i;}
void print() { cout << "I'm a D. my num=" << num<< endl; }
int num;
};

int main()
{
vector<D> V;

V.push_back(D(1));
V.push_back( D(2));
V.push_back( D());
V.push_back( D(3));

for_each(V.begin(), V.end(), mem_fun_ref(&D::print));
}

一切都OK了。

mem_fun对于一些多态的虚函数也十分有用,注意看下面的例子:

struct B {
virtual void print() = 0;
};

struct D1 : public B {
void print() { cout << "I'm a D1" << endl; }
};

struct D2 : public B {
void print() { cout << "I'm a D2" << endl; }
};

int main()
{
vector<B*> V;

V.push_back(new D1);
V.push_back(new D2);
V.push_back(new D2);
V.push_back(new D1);

for_each(V.begin(), V.end(), mem_fun(&B::print));
}

理解了吗? 有更多的兴趣,可以看看mem_fun1和mem_fun1_ref,他们可以使用绑定一个参数的类成员。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: