STL中mem_fun, mem_fun_ref用法

1.引言

  先看一个STL中for_each的用法:

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <functional>
 5 #include <iterator>
 6 using namespace std;
 7 class Test
 8 {
 9 public:
10     Test(int _data = 0):data(_data){}
11
12     void print(){cout<<"i am class Test"<< data<<endl;}
13     void add(int b){ data += b;}
14     int data;
15 };
16
17 int val[] = {0,1,2,3,4,5,6};
18 int main()
19 {    Test t(3);
20     vector<int> ivec(val,val+7);
21     vector<Test> tvec;
22     copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(tvec));
23     /*这个地方是将int隐式转化为Test类型了,与本主题无关,使用ctrl+z结束输入*/
24     for_each(tvec.begin(),tvec.end(),Test::print);
25     for_each(ivec.begin(),ivec.end(),t.add);
26 }

  我们的目的很明显:

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源码

1 // TEMPLATE FUNCTION mem_fun_ref
2 template<class _Result,class _Ty>
3 inline mem_fun_ref_t<_Result, _Ty> mem_fun_ref(_Result (_Ty::*_Pm)()){
4     // return a mem_fun_ref_t functor adapter
5     return (std::mem_fun_ref_t<_Result, _Ty>(_Pm));
6 }    

  mem_fun_ref准确的说是个函数,他返回的是mem_fun_ref_t类。

  函数的声明为:

1 template<class _Result, class _Ty>
2 inline mem_fun_ref_t<_Result,_Ty> mem_fun_ref(_Result (_Ty::*_Pm)());

  mem_fun_ref函数的形参是一个类成员函数指针为_Result (_Ty::*_Pm)(), 注意最右边的空() ,  这个函数指针无参数,类为_Ty,类成员函数指针为_Pm,成员函数返回值为_Result,因此:

1 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的定义

 1 // TEMPLATE CLASS mem_fun_ref_t
 2 template<class _Result,class _Ty>
 3 class mem_fun_ref_t:public unary_function<_Ty, _Result>
 4 {    // functor adapter (*left.*pfunc)(), non-const *pfunc
 5 public:
 6     explicit mem_fun_ref_t(_Result (_Ty::*_Pm)()):_Pmemfun(_Pm){
 7                  // construct from pointer
 8     }
 9          //重要的是这一句,,注意了!
10     _Result operator()(_Ty& _Left) const{
11                 // call function
12         return ((_Left.*_Pmemfun)());
13     }
14 private:
15     _Result (_Ty::*_Pmemfun)();    // the member function pointer
16 };

  模板类mem_fun_ref_t中前面已经分析了对于:

1 mem_fun_ref(&Test::print)  

  来说_Result = void ,_Ty = Test,构造函数将_Pmemfun指针指向&Test::print成员函数,该类重载了operator() 故为仿函数(functor),对于最前面的for_each,第三个实参为函数指针_pfn,for_each内部会这样调用_pfn( Test),那么应用到这里,即

1 _Result operator()(_Ty &_Left)const{
2     return ((_Left.*_Pmemfun)());
3 } 

  每次传给这个仿函数一个functor, ,然后成为变量_Left , 调用 _Left.print  就达到了目的( 对tvec里面的每一个Test类变量 ,  都调用自身的print成员函数。

  当对于vector<Test*> ptvec;时 , 就得用mem_fun,可以自已分析下源码。

3.mem_fun1_ref

  上述例子中的第二个for_each解决方案, 就得使用me_fun1_ref 这个会返回带一个参数的funtor(仿函数)。

4.修改后的程序

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <functional>
 5 #include <iterator>
 6 using namespace std;
 7 class Test
 8 {
 9 public:
10     Test(int _data = 0):data(_data){}
11
12     void print(){cout<<"i am class Test"<< data<<endl;}
13     void add(int b){ data += b;}
14     int data;
15 };
16
17 int val[] = {0,1,2,3,4,5,6};
18 int main()
19 {    Test t(3);
20     vector<int> ivec(val,val+7);
21     vector<Test> tvec;
22     copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(tvec));//这个地方是将int隐式转化为Test类型了,与本主题无关
23     for_each(tvec.begin(),tvec.end(),mem_fun_ref(&Test::print));
24     for_each(ivec.begin(),ivec.end(),bind1st(mem_fun1_ref(&Test::add),t)); //此句在vs2008上通不过,,别的IDE,codeblock上面没mem_fun1_ref这个东西,,看vs2010行不行了
25 }

原文链接:http://blog.csdn.net/fdl19881/article/details/6938772

时间: 2024-08-11 22:13:19

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的用法 分类: C++2006-11-21 09:11 5244人阅读 评论(8) 收藏 举报 怎么对容器中的所有对象都进行同一个操作?我们可能首先想到的是用循环来实现.    比如有如下的一个类: class ClxECS{public:    int DoSomething()     {         //

(转)STL中set的用法

转载自here 1.关于set map容器是键-值对的集合,好比以人名为键的地址和电话号码.相反地,set容器只是单纯的键的集合.例如,某公司可能定义了一个名为bad_checks的set容器,用于记录曾经给本公司发空头支票的客户.当想知道一个值是否存在时,使用set容器是最适合的.除了两种例外情况,set容器支持大部分的map操作,这两种例外是:set不支持下标操作,而且也没有mapped_type类型,在set容器中,value_type不是pair类型,而是与key_type相同的类型.它

(转)C++ STL中list的用法

博客搬家啦http://t.cn/RvFZs2c STL中list的用法 C++ Lists(链表) 赋值(assign) 语法: void assign( input_iterator start, input_iterator end ); void assign( size_type num, const TYPE &val ); assign()函数以迭代器start和end指示的范围为list赋值或者为list赋值num个以val为值的元素. 相关主题: insert(), back

STL中map的用法

map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道.这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处. 下面举例说明什么是一对一的数据映射.比如一个班级中,每个学生的学号跟他的姓名就存在着一一

STL中的map用法详解

STL中map用法详解 说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂.本人水平有限,不当之处,望大家辅正. 一.map概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道.这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),

STL中的Set用法(详+转)

set是STL中一种标准关联容器(vector,list,string,deque都是序列容器,而set,multiset,map,multimap是标准关联容器),它底层使用平衡的搜索树——红黑树实现,插入删除操作时仅仅需要指针操作节点即可完成,不涉及到内存移动和拷贝,所以效率比较高.set,顾名思义是“集合”的意思,在set中元素都是唯一的,而且默认情况下会对元素自动进行升序排列,支持集合的交(set_intersection),差(set_difference) 并(set_union),

STL中mem_fun和mem_fun_ref的用法

怎么对容器中的所有对象都进行同一个操作?我们可能首先想到的是用循环来实现.    比如有如下的一个类: class ClxECS{public:    int DoSomething()     {         // 这里以输出一句话来代替具体的操作        cout << "Output from method DoSomething!" << endl;         return 0;     };}; 现在定义如下一个vector: vect

STL中vector的用法

vector应该是STL种最常用的容器了,可以当做数组来看待,只不过vector的元素更丰富,不仅仅是数据元素,还可以是结构体 1.vector的创建和初始化 vector <double> v;//创建一个double类型的vector vector <int> v2(5);//创建一个含有5个元素的int型vector,初始值默认为0 vector <int> v3(4,7);//创建一个含有4个元素的int型vector,初始值都为7 当然也可以通过在尾部添加元素

STL中nth_element的用法

nth_element函数原型有四个,详细我就不一一累赘了,我们就用最普通的用法寻找第k位置的元素. 函数用法为:nth_element(first,kth,end). first,last 第一个和最后一个迭代器,也可以直接用数组的位置. kth,要定位的第k个元素,能对它进行随机访问. 将第k_th元素放到它该放的位置上,左边元素都小于等于它,右边元素都大于等于它. 例如: 1 vector<int> a(9); 2 for(int i = 0; i < 9; i++) 3 a[i]