STL中mem_fun, mem_fun_ref用法

來源:互聯網
上載者:User

標籤:

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_ref2.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_ref2 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 adapter5     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 function12         return ((_Left.*_Pmemfun)());13     }14 private:15     _Result (_Ty::*_Pmemfun)();    // the member function pointer16 };
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,可以自已分析下源碼。

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 }
View Code

 

原文連結:http://blog.csdn.net/fdl19881/article/details/6938772

STL中mem_fun, mem_fun_ref用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.