• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            loop_in_codes

            低調做技術__歡迎移步我的獨立博客 codemaro.com 微博 kevinlynx

            分析stl function objects模塊


            從SGI的STl文檔來看,STL functor(function object)模塊主要分為兩個部分:預先定義的functor
            以及functor adaptors。除此之外,為了使客端程序員寫出適用于functor adaptor的functor,STL
            又定義了一系列基本上只包含typedef的空類型(例如unary_function)。用戶只需要派生這些類,即
            可讓自己寫的functor被functor adaptor使用。以下稱類基類型為base functor。

            base functor包括: unary_function, binary_function,分別表示只有一個參數的函數和有兩個參數
            的函數。實際上STL里還有一個所謂的generator,代表沒有參數的函數。因為STL泛型算法一般最多
            只會使用兩個參數的函數,所以這里并沒有定義更多參數的base functor。

            可被functor adaptor使用的functor又稱為adaptable function,根據參數的個數,會被命名為諸如
            adaptable unary function, adaptable binary function。

            一個返回值為bool的functor又被稱為predicate,可被用于functor adaptor的predicate被稱為
            adaptable predicate。其實所謂的adaptable,只需要在類型內部typedef一些類型即可,一般包括
            first_argument_type, second_argument_type, result_type。functor adaptor會使用這些定義。

            預定義的functors都是些很簡單的functor,基本上就是封裝諸如plus, minus, equal_to之類的算術
            運算,列舉一個predefined functor的代碼:
             

            template <class _Tp>
              
            struct plus : public binary_function<_Tp, _Tp, _Tp>
              
            {
                 _Tp 
            operator()(const _Tp& __x, const _Tp& __y) const   
                 

                     
            return __x + __y; 
                 }
                
              }
            ;


            因為從binary_function(即我所謂的base functor)派生,因此這些predefined functor也是adaptable
            function。

            functor adaptors里有很多有趣的東西,其實functor adaptor也是一些functor(從SGI的觀點來看,一般
            的C函數,函數指針都算作functor)。所不同的是,他們通常會適配(adapt)一種functor到另一種。例如:
            std::binder1st,嚴格地說它是一個函數模板,它會把一個adaptable binary function轉換為一個
            adaptable unary function,并綁定一個參數。又如: std::ptr_fun,它會將一個只有一個參數的C函數
            適配成一個pointer_to_unary_function的functor。

            下面列舉一些具體的代碼:
            關于base functor,基本上就只有unary_function, binary_function :
             

            template <class _Arg, class _Result>
              
            struct unary_function
              
            {
                  typedef _Arg argument_type;                    
                  typedef _Result result_type; 
              }
            ;
              


            關于predefined functor,如之前列舉的plus一樣,再列舉一個:

            template <class _Tp>
             
            struct greater : public binary_function<_Tp, _Tp, bool>
             
            {      
                 
            bool operator()(const _Tp& __x, const _Tp& __y) const
                 

                     
            return __x > __y; 
                 }
                
             }
            ;

             
            關于functor adaptors,也是我覺得比較有趣的部分,多列舉幾個:

            template <class _Operation, class _Tp>
              inline binder1st
            <_Operation>
              bind1st(
            const _Operation& __fn, const _Tp& __x)
              
            {
                  typedef typename _Operation::first_argument_type _Arg1_type;
                  
            return binder1st<_Operation>(__fn, _Arg1_type(__x));
              }

              

             
            bind1st返回的binder1st定義為:

             template <class _Operation>
              
            class binder1st : public unary_function<typename _Operation::second_argument_type, 
              typename _Operation::result_type
            >
              
            {
              
            protected:
                  _Operation op;
                  typename _Operation::first_argument_type value;
              
            public:
                  binder1st(
            const _Operation& __x, const typename _Operation::first_argument_type& __y): 
                    op(__x), value(__y) 
                  
            {}
                  typename _Operation::result_type 
                  
            operator()(const typename _Operation::second_argument_type& __x) const
                  

                     
            return op(value, __x); 
                  }

                 typename _Operation::result_type 
                 
            operator()(typename _Operation::second_argument_type& __x) const
                 

                    
            return op(value, __x); 
                 }

              }
            ;

             
            值得一提的是,ptr_fun以及相關的pointer_to_unary_function, pointer_to_binary_function,基本上
            就是用來綁定C函數的組件,不過這里采用了很基礎的模板技術,因此只實現了綁定一個參數和兩個參數
            的C函數。這種組件類似于loki中的functor,以及boost中的bind,只是功能弱很多。與之相關的還有
            mem_fun, mem_fun_ref, mem_fun1, mem_fun1_ref等,這些都是用于綁定成員函數的。另一方面,與其說
            是綁定,還不如說適配,即將函數適配為functor(特指重載operator()的類)。( Mem_fun_t is an adaptor
             for member functions )采用這些(ptr_fun, mem_fun之類的東西)組件,客端程序員可以很容易地將各種
            運行體(Kevin似乎很喜歡發明各種名字)(C函數、成員函數)適配成functor,從而與STL泛型算法結合。
            例如, SGI文檔中給出的mem_fun例子:

             

            struct B {
             
            virtual void print() = 0;
            }
            ;

            struct D1 : public B {
             
            void print() { cout << "I'm a D1" << endl; }
            }
            ;

            struct D2 : public B {
             
            void print() { cout << "I'm a D2" << endl; }
            }
            ;

            int main()
            {
             vector
            <B*> V;

             V.push_back(
            new D1);
             V.push_back(
            new D2);
             V.push_back(
            new D2);
             V.push_back(
            new D1);

             for_each(V.begin(), V.end(), mem_fun(
            &B::print));
            }

             

             

            注:以上分析基于dev-cpp中自帶的stl,源代碼見stl_functional.h。

             

             

            posted on 2008-03-13 13:31 Kevin Lynx 閱讀(2529) 評論(2)  編輯 收藏 引用 所屬分類: c/c++

            評論

            # re: 分析stl function objects模塊 2008-03-13 13:41 cppexplore

            這個要頂!  回復  更多評論   

            # re: 分析stl function objects模塊 2008-03-14 12:41 夢在天涯

            高!  回復  更多評論   

            久久久久99精品成人片三人毛片| 国产精品视频久久久| 国产午夜精品理论片久久| 国产国产成人久久精品| 久久综合九色综合久99| 免费精品久久天干天干| 久久亚洲美女精品国产精品| 久久99精品综合国产首页| 办公室久久精品| 人人狠狠综合久久88成人| 国产精品九九久久免费视频 | 999久久久免费国产精品播放| 久久99中文字幕久久| 久久综合鬼色88久久精品综合自在自线噜噜 | 久久精品国产精品青草app| 久久99精品国产麻豆蜜芽| 99久久精品免费看国产一区二区三区| 久久不见久久见免费视频7| 国产一区二区精品久久凹凸| 色婷婷久久综合中文久久蜜桃av| 国产精品丝袜久久久久久不卡| 精品多毛少妇人妻AV免费久久 | 97久久精品人人澡人人爽| 久久久久久久亚洲Av无码| 久久福利片| 亚洲国产精品久久久久网站| 亚洲午夜久久久影院| 久久人妻少妇嫩草AV无码蜜桃| 国产一级做a爰片久久毛片| 99久久精品免费看国产一区二区三区 | 99久久国产综合精品网成人影院 | 日韩一区二区久久久久久| 久久久亚洲裙底偷窥综合| 久久久久亚洲AV无码去区首| 久久精品一区二区| 精品久久一区二区三区| 国内精品伊人久久久久AV影院| 婷婷综合久久中文字幕蜜桃三电影| 久久精品国产黑森林| 久久久精品国产亚洲成人满18免费网站 | 婷婷久久久亚洲欧洲日产国码AV |