STL中mem_fun, mem_fun_ref用法,mem_funmem_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 }
View Code
我們的目的很明顯:
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 }
View Code
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)());
View Code
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 };
View Code
模板類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 }
View Code
每次傳給這個仿函數一個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.修改後的程序

![]()
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 {
20 Test t(3);
21 vector<int> ivec(val,val+7);
22 vector<Test> tvec;
23 copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(tvec));//這個地方是將int隱式轉化為Test類型了,與本主題無關
24 for_each(tvec.begin(),tvec.end(),mem_fun_ref(&Test::print));
25 for_each(ivec.begin(),ivec.end(),bind1st(mem_fun1_ref(&Test::add),t)); //此句在vs2008上通不過,,別的IDE,codeblock上面沒mem_fun1_ref這個東西,,看vs2010行不行了
26 }
View Code
原文鏈接:http://blog.csdn.net/fdl19881/article/details/6938772