STL中mem_fun, mem_fun_ref用法
1.引言
先看一个STL中for_each的用法:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
class Test
{
public:
Test(int _data = ):data(_data){} void print(){cout<<"i am class Test"<< data<<endl;}
void add(int b){ data += b;}
int data;
}; int val[] = {,,,,,,};
int main()
{ Test t();
vector<int> ivec(val,val+);
vector<Test> tvec;
copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(tvec));
/*这个地方是将int隐式转化为Test类型了,与本主题无关,使用ctrl+z结束输入*/
for_each(tvec.begin(),tvec.end(),Test::print);
for_each(ivec.begin(),ivec.end(),t.add);
}
我们的目的很明显:
1.输出vector<Test>的所有变量,通过调用成员变量print函数
2.将ivec中的七个数加上类变量t中,使用t.add(int)函数
但是上面的1,2两句确怎么也无法成功 (当然了可以用for循环来做,,但这就违背了Template的初忠了)
2.mem_fun, mem_fun_ref
2.1 解决方法
for_each(tvec.begin(),tvec.end(),&Test::print);
改写成
for_each(tvec.begin(),tvec.end(),mem_fun_ref(&Test::print));
这样就能成功达到我们的第一个目的了
2.2 mem_fun_ref分析
2.2.1 mem_fun_ref源码
// TEMPLATE FUNCTION mem_fun_ref
template<class _Result,class _Ty>
inline mem_fun_ref_t<_Result, _Ty> mem_fun_ref(_Result (_Ty::*_Pm)()){
// return a mem_fun_ref_t functor adapter
return (std::mem_fun_ref_t<_Result, _Ty>(_Pm));
}
mem_fun_ref准确的说是个函数,他返回的是mem_fun_ref_t类,函数的声明为:
template<class _Result, class _Ty>
inline mem_fun_ref_t<_Result,_Ty> mem_fun_ref(_Result (_Ty::*_Pm)());
mem_fun_ref函数的形参是一个类成员函数指针为_Result (_Ty::*_Pm)(), 注意最右边的空() , 这个函数指针无参数,类为_Ty,类成员函数指针为_Pm,成员函数返回值为_Result,因此:
mem_fun_ref(&Test::print)
这句将返回一个mem_fun_ref_t的类 , 其中:
(1)_Result为Test::print的返回值,即void
(2)_Ty 为Test::print的类,即Test
(3)_Pm为类成员函数指针,即&print
2.2.2 class mem_fun_ref_t的定义
// TEMPLATE CLASS mem_fun_ref_t
template<class _Result,class _Ty>
class mem_fun_ref_t:public unary_function<_Ty, _Result>
{ // functor adapter (*left.*pfunc)(), non-const *pfunc
public:
explicit mem_fun_ref_t(_Result (_Ty::*_Pm)()):_Pmemfun(_Pm){
// construct from pointer
}
//重要的是这一句,,注意了!
_Result operator()(_Ty& _Left) const{
// call function
return ((_Left.*_Pmemfun)());
}
private:
_Result (_Ty::*_Pmemfun)(); // the member function pointer
};
模板类mem_fun_ref_t中前面已经分析了对于:
mem_fun_ref(&Test::print)
来说_Result = void ,_Ty = Test,构造函数将_Pmemfun指针指向&Test::print成员函数,该类重载了operator(),故为仿函数(functor),对于最前面的for_each,第三个实参为函数指针_pfn,for_each内部会这样调用_pfn( Test),那么应用到这里,即
_Result operator()(_Ty &_Left)const{
return ((_Left.*_Pmemfun)());
}
每次传给这个仿函数一个functor, ,然后成为变量_Left , 调用 _Left.print 就达到了目的( 对tvec里面的每一个Test类变量, 都调用自身的print成员函数)。
当对于vector<Test*> ptvec;时 , 就得用mem_fun,可以自已分析下源码。
2.3 mem_fun与mem_fun_ref
mem_fun_ref的作用和用法跟mem_fun一样,唯一的不同就是:当容器中存放的是对象实体的时候用mem_fun_ref,当容器中存放的是对象的指针的时候用mem_fun。
3.mem_fun1_ref
上述例子中的第二个for_each解决方案, 就得使用me_fun1_ref 这个会返回带一个参数的funtor(仿函数)。
4.修改后的程序
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
class Test
{
public:
Test(int _data = ):data(_data){} void print(){cout<<"i am class Test"<< data<<endl;}
void add(int b){ data += b;}
int data;
}; int val[] = {,,,,,,};
int main()
{
Test t();
vector<int> ivec(val,val+);
vector<Test> tvec;
copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(tvec));//这个地方是将int隐式转化为Test类型了,与本主题无关
for_each(tvec.begin(),tvec.end(),mem_fun_ref(&Test::print));
for_each(ivec.begin(),ivec.end(),bind1st(mem_fun1_ref(&Test::add),t)); //此句在vs2008上通不过,,别的IDE,codeblock上面没mem_fun1_ref这个东西,,看vs2010行不行了
}
原文链接:http://blog.csdn.net/fdl19881/article/details/6938772
STL中mem_fun, mem_fun_ref用法的更多相关文章
- STL中mem_fun与mem_fun_ref的区别[转]
http://www.cnblogs.com/Purple_Xiapei/archive/2012/05/27/2520483.html STL中mem_fun和mem_fun_ref的用法 分类: ...
- STL中mem_fun和mem_fun_ref的用法
例如:假设有如下的代码: class Employee { public: int DoSomething(){} } std::vector<Employee> Emps; 假设我们要调 ...
- STL中map的用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
- (转)STL中set的用法
转载自here 1.关于set map容器是键-值对的集合,好比以人名为键的地址和电话号码.相反地,set容器只是单纯的键的集合.例如,某公司可能定义了一个名为bad_checks的set容器,用于记 ...
- STL中的Set用法(详+转)
set是STL中一种标准关联容器(vector,list,string,deque都是序列容器,而set,multiset,map,multimap是标准关联容器),它底层使用平衡的搜索树——红黑树实 ...
- 【转】STL中mem_fun和mem_fun_ref的用法及区别
原文:http://www.cppblog.com/mysileng/archive/2012/12/25/196615.html 引子: 怎么对容器中的所有对象都进行同一个操作?我们可能首先想到的是 ...
- STL中erase()的用法
erase()是STL提供的容器中比较常用的方法之一,它的功能是删除容器中的某些元素,其中它的函数原型如下: 1.有两个参数,且参数类型都是size_t型: string& erase ( s ...
- STL中bitset的用法
终于又来写博客了 == bitset存储的是二进数位,就和一个bool性数组差不多.用法上和数组的操作方式也差不多. 每位只占一个字节,大大优化了空间,可以通过数组形式访问. bitset定义 可以用 ...
- STL中nth_element的用法
nth_element函数原型有四个,详细我就不一一累赘了,我们就用最普通的用法寻找第k位置的元素. 函数用法为:nth_element(first,kth,end). first,last 第一个和 ...
随机推荐
- atoi函数的实现——面试
主要考虑,字符串中是否有非法字符,字符串是否有溢出控制 #include<stdio.h> int myatoi(const char *str){ ,ret=,i=; if(str[i] ...
- 论BOM管理的若干重要问题
在制造业信息化建设过程中,BOM管理的意义已经被广泛认同与重视.BOM管理是企业产品数据管理领域要解决的核心问题,对制造企业的信息流和业务流程具有重要影响,可以说企业BOM数据架构已是企业架构的一部分 ...
- python中scipy学习——随机稀疏矩阵及操作
1.生成随机稀疏矩阵: scipy中生成随机稀疏矩阵的函数如下: scipy.sparse.rand(m,n,density,format,dtype,random_state) 1 参数介绍: 参数 ...
- PAT甲级 1002 A+B for Polynomials (25)(25 分)
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...
- 杂项-Server:Serv-U
ylbtech-杂项-Server:Serv-U Serv-U FTP Server,是一种被广泛运用的FTP服务器端软件,支持3x/9x/ME/NT/2K等全Windows系列.可以设定多个FTP服 ...
- 分析java类的初始化契机
分析java类的静态成员变量初始化先于非静态成员变量 依上图中当class字节码文件被jvm虚拟机加载到内存中依次经过 连接 验证:对字节码进行验证 准备:给静态变量分配内存并赋予变量类型各自的默 ...
- POJ-3273 Monthly Expense (最大值最小化问题)
/* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...
- [Python] Regular Expressions
1. regular expression Regular expression is a special sequence of characters that helps you match or ...
- angularjs之向下一个页面传参
原理: 1.在a标签跳转时,连接后增加一个参数值 2.在路由中接收 3.在控制器中接收 实现: 1.<a href="#/list/{{val.id}}"> 2.在js ...
- C/S软件的自动升级部署
升级的原理有好几个,首先无非是将现有版本与最新版本作比较,发现最新的则提示用户是否升级.当然也有人用其它属性比较的,例如:文件大小,或者更新日期.而实现的方法呢? 在.Net时代,我们就有了更多的选择 ...