stl中函数适配器mem_fun的用法

vcgaoshou 2008-11-07 01:43:51
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
class CShape{
public:
virtual void Disp(){cout<<"Disp()"<<endl;}
};
class CRect:public CShape{
public:
virtual void Disp(){cout<<"CRect()"<<endl;}
};
class CCircle:public CShape{
public:
virtual void Disp(){cout<<"CCircle()"<<endl;}
};
void main(){
int i;
vector<CShape*> v;
v.push_back(new CShape);
v.push_back(new CRect);
v.push_back(new CCircle);
for_each(v.begin(),v.end(),mem_fun(&CShape::Disp));
for(i=0;i<3;i++){
delete v[i];
v.pop_back() ;
}
}
编译后产生error C2562: '()' : 'void' function returning a value
是由于void产生的,那么对于返回类型为void的成员函数如何使用mem_fun呢
另外对于具有一个参数的成员函数,for_each算法中mem_fun后如何体现CShape::Disp有一个参数
另外还请各位有学习函数对象,函数适配器例子的网站
...全文
240 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuhb95083023 2008-11-07
  • 打赏
  • 举报
回复
SGI STL中的代码有void类型的偏特化版本,lz的编译器不支持类模板偏特化,所以没有对应实现,我估计lz用的vc6或者vc7.0,vc7.1以上才支持

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

template <class _Tp>
class const_mem_fun_t<void, _Tp> : public unary_function<const _Tp*,void> {
public:
explicit const_mem_fun_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}
void operator()(const _Tp* __p) const { (__p->*_M_f)(); }
private:
void (_Tp::*_M_f)() const;
};
xxgamexx 2008-11-07
  • 打赏
  • 举报
回复
2个参数是不是?



#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;


class test
{
public :
int fun(int param,int param2) { return param+param2; }
};

int main()
{
vector<test*> vt;

vt.push_back(new test);
vt.push_back(new test);
vt.push_back(new test);
vt.push_back(new test);

for_each(vt.begin(), vt.end(), bind2nd(mem_fun1(&test::fun), 100,100));

return 0;
}



jia_xiaoxin 2008-11-07
  • 打赏
  • 举报
回复
楼主是不是用的VC6等的低版本的编译器,VC6对STL的支持不好,很多都会报错,建议楼主在VC2005上编译使用。
for_each的传入函数不一定非要有参数。
Fighting Horse 2008-11-07
  • 打赏
  • 举报
回复
没有问题,可能编译器支持不够
fallening 2008-11-07
  • 打赏
  • 举报
回复
建议换用更加强大,更加方便的boost::bind
星羽 2008-11-07
  • 打赏
  • 举报
回复
带参数的例子


#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;


class test
{
public :
int fun(int param) { cout<<param<<endl; return 0; }
};

int main()
{
vector<test*> vt;

vt.push_back(new test);
vt.push_back(new test);
vt.push_back(new test);
vt.push_back(new test);

for_each(vt.begin(), vt.end(), bind2nd(mem_fun1(&test::fun), 100));

return 0;
}
星羽 2008-11-07
  • 打赏
  • 举报
回复

void main(){
int i;
vector <CShape*> v;
v.push_back(new CShape);
v.push_back(new CRect);
v.push_back(new CCircle);
for_each(v.begin(),v.end(),mem_fun(&CShape::Disp));
for(i=0;i <3;i++){
delete v[i];
// v.pop_back() ; 这里不能这么做
}
v.clear(); // 在这里再清空
}
星羽 2008-11-07
  • 打赏
  • 举报
回复
有一个参数的用

bind2nd 和 mem_fun1
taodm 2008-11-07
  • 打赏
  • 举报
回复
买本《STL源码剖析》,上面讲得太详细了。

64,701

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧