• <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>

            woaidongmao

            文章均收錄自他人博客,但不喜標(biāo)題前加-[轉(zhuǎn)貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評(píng)論 - 661, 引用 - 0
            數(shù)據(jù)加載中……

            分析stl function objects模塊

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

            base functor包括: unary_function, binary_function,分別表示只有一個(gè)參數(shù)的函數(shù)和有兩個(gè)參數(shù)
            的函數(shù)。實(shí)際上STL里還有一個(gè)所謂的generator,代表沒有參數(shù)的函數(shù)。因?yàn)镾TL泛型算法一般最多
            只會(huì)使用兩個(gè)參數(shù)的函數(shù),所以這里并沒有定義更多參數(shù)的base functor。

            可被functor adaptor使用的functor又稱為adaptable function,根據(jù)參數(shù)的個(gè)數(shù),會(huì)被命名為諸如
            adaptable unary function, adaptable binary function。

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

            預(yù)定義的functors都是些很簡(jiǎn)單的functor,基本上就是封裝諸如plus, minus, equal_to之類的算術(shù)
            運(yùn)算,列舉一個(gè)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;
                 }
               
              }
            ;


            因?yàn)閺腷inary_function(即我所謂的base functor)派生,因此這些predefined functor也是adaptable
            function。

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

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

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


            關(guān)于predefined functor,如之前列舉的plus一樣,再列舉一個(gè):

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

             
            關(guān)于functor adaptors,也是我覺得比較有趣的部分,多列舉幾個(gè):

            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以及相關(guān)的pointer_to_unary_function, pointer_to_binary_function,基本上
            就是用來綁定C函數(shù)的組件,不過這里采用了很基礎(chǔ)的模板技術(shù),因此只實(shí)現(xiàn)了綁定一個(gè)參數(shù)和兩個(gè)參數(shù)
            的C函數(shù)。這種組件類似于loki中的functor,以及boost中的bind,只是功能弱很多。與之相關(guān)的還有
            mem_fun, mem_fun_ref, mem_fun1, mem_fun1_ref等,這些都是用于綁定成員函數(shù)的。另一方面,與其說
            是綁定,還不如說適配,即將函數(shù)適配為functor(特指重載operator()的類)。( Mem_fun_t is an adaptor
            for member functions )采用這些(ptr_fun, mem_fun之類的東西)組件,客端程序員可以很容易地將各種
            運(yùn)行體(Kevin似乎很喜歡發(fā)明各種名字)(C函數(shù)、成員函數(shù))適配成functor,從而與STL泛型算法結(jié)合。
            例如, 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-08-31 21:42 肥仔 閱讀(315) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++ 模板

            久久精品国产免费观看| 亚洲中文字幕无码一久久区| 日本精品久久久久中文字幕8 | 亚洲一本综合久久| 久久99精品久久久久久水蜜桃| 久久久久久亚洲精品不卡| 中文国产成人精品久久不卡| 99久久精品国产麻豆| 日韩欧美亚洲综合久久影院Ds | 思思久久精品在热线热| 久久水蜜桃亚洲av无码精品麻豆 | 久久久无码人妻精品无码| 亚洲国产成人久久综合碰碰动漫3d | 99久久精品国产综合一区| 久久人妻少妇嫩草AV蜜桃| 久久综合丁香激情久久| 成人综合久久精品色婷婷| 国产福利电影一区二区三区,免费久久久久久久精 | 久久成人国产精品一区二区| 亚洲AV无码久久寂寞少妇| 色天使久久综合网天天| 91精品免费久久久久久久久| 狼狼综合久久久久综合网| 中文成人无码精品久久久不卡 | 国产精品一久久香蕉国产线看| 大香伊人久久精品一区二区 | 99久久人妻无码精品系列蜜桃| 99久久免费国产精品特黄| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 久久精品一本到99热免费| 精品久久久久久久久久中文字幕| 久久婷婷成人综合色综合| 亚洲国产精品一区二区久久hs | 久久精品国产亚洲AV麻豆网站| A级毛片无码久久精品免费| 要久久爱在线免费观看| 久久97久久97精品免视看秋霞 | 久久精品一区二区三区AV| 偷窥少妇久久久久久久久| 国内精品九九久久精品| 久久久久久精品无码人妻|