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

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

            STL函數(shù)對象及函數(shù)對象適配器

            STL函數(shù)對象及函數(shù)對象適配器


            函數(shù)對象Functor

                STL中提供了一元和二元函數(shù)的兩種Functor,通過unary_function和binary_function提供了這兩種不同參數(shù)數(shù)量的Functor的基本結(jié)構(gòu),在這兩個類型中,分別內(nèi)嵌定義一元和二元函數(shù)操作在模版推演的時候需要用到的typedef.
             

            //一元函數(shù)的定義為
            template<class _A, class _R>
            struct unary_function {
             typedef _A argument_type;
             typedef _R result_type;
            };


            //二元函數(shù)的定義為
            template<class _A1, class _A2, class _R>
             struct binary_function {
             typedef _A1 first_argument_type;
             typedef _A2 second_argument_type;
             typedef _R result_type;
            };

            其他的一元和二元Functor可以從這兩個基本結(jié)構(gòu)繼承,同時也就可以推演出函數(shù)的參數(shù)和返回值的類型,STL在上述這兩個結(jié)構(gòu)的基礎(chǔ)上,實現(xiàn)了很多一元和二元的Functor.

            //一元
            negate

            //二元
            plus
            minus
            multiplies
            divides
            modulus
            equal_to
            not_equal_to
            greater
            greater_equal
            less
            less_equal
            logical_and
            logical_or
            logical_not

            上面的這些Functor都是基于模版實現(xiàn)的,可以象下面那樣使用的方式:

            plus<int> int_plus;
            cout << int_plus(111,222) << endl;


            函數(shù)對象適配器

            函數(shù)對象適配器的作用就是使函數(shù)轉(zhuǎn)化為函數(shù)對象,或是將多參數(shù)的函數(shù)對象轉(zhuǎn)化為少參數(shù)的函數(shù)對象。


            1)bind

            bind1st  //通過綁定第一個參數(shù),使二元的函數(shù)對象轉(zhuǎn)化為一元的函數(shù)對象
            bind2nd  //通過綁定第二個參數(shù),使二元的函數(shù)對象轉(zhuǎn)化為一元的函數(shù)對象
            not1     //對一元的函數(shù)對象取反
            not2     //對二元的函數(shù)對象取反

            使用的方式:
            bind1st( less<int>(), 10)(20);
            not2( less<int() )(10,20);

            這些Functor看起來好像好像用處不大,但是在和STL中的容器和算法結(jié)合在一起使用的時候,就會使得程序顯得很簡潔.

            int i;   
            vector<int> lv;
            for(i = 0; i < 100; i++)
            {
                lv.push_back(i);
            }
            //對vector中小于20的數(shù)進行記數(shù)
            cout << count_if(lv.begin(), lv.end(), bind2nd(less<int>(), 20)) << endl;

            //由大到小排序
            sort(lv.begin(), lv.end(), not2(less<int>()) ) ;
            for (i = 0; i < 100; i++)
            {
                cout << lv.at(i) << endl;
            }


            2)ptr_fun

            ptr_fun是指將現(xiàn)有的函數(shù)轉(zhuǎn)換為Functor的功能.在STL中提供了這個功能的Functor,就是pointer_to_unary_function和pointer_to_binary_function這兩個類,這兩個類對應(yīng)一元

            和二元兩種函數(shù),也就是說,對于調(diào)用參數(shù)為3個或者多于3個的函數(shù),STL提供的Functor類,無法配接.

            基本使用方法:

            int u_func(int a)
            {
                int ret = a;
                return ret;
            }   

            int b_func(int a,int b)
            {
                return a+b;
            }

            void call()
            {
             pointer_to_unary_function<int,int> uf(u_func);
                cout << uf(100) << endl;
               
                pointer_to_binary_function<int,int,int> bf(b_func);
                cout << bf(111,222) << endl;

             //或者
             cout << ptr_fun(u_func)(100) << endl;
                cout << ptr_fun(b_func)(111,222) << endl;

            }
            可以看到,上面的方法改進了原先C和C++中通過函數(shù)指針來間接調(diào)用函數(shù)的方法,將函數(shù)指針封裝到了類中.


            問題:

            第一部分中的Functor中是自己定義操作符(),但是在ptr_fun中,是將已經(jīng)有的function轉(zhuǎn)為Functor調(diào)用就會存在一個調(diào)用方式的問題.

            c++中的函數(shù),按調(diào)用方式可以分為__cdecl, __stdcall,__fastcall 三種,ptr_fun如何正確的識別給定的function的調(diào)用方式就會有問題.

            其中:
            vc6中的STL的ptr_fun代碼中,統(tǒng)一將function認(rèn)為是__cdecl調(diào)用方式. 而Dev-cpp中使用的SGI的代碼中沒有明確指明函數(shù)的調(diào)用方式,所以將使用編譯器的確省設(shè)置.
            但是如果將上面的b_func函數(shù)改為
            int __stdcall b_func(int a,int b)
            {
                return a+b;
            }
            上面的使用代碼在DEV-CPP中無法編譯通過.

            3)mem_fun

            mem_fun是將某個類中的成員函數(shù)轉(zhuǎn)變?yōu)镕unctor的功能.

            一般的使用方法
            struct mem_fun_struct
            {
                int n_mem_fun() {
                    cout << "mem_fun_struct::n_mem_fun()" << endl;
                    return 0;
                }   
               
                int u_mem_fun(int a) {
                    cout << "mem_fun_struct::u_mem_fun(int) " << a << endl;
                    return a;
                }   
               
                int b_mem_fun(int a,int b) {
                    cout << "mem_fun_struct::b_mem_fun(int,int)" << a << " " << b << endl;
                    return a+b;
                }   
            };

            void call()
            {
             mem_fun_struct ls;
                mem_fun(&mem_fun_struct::n_mem_fun)(&ls);
                mem_fun(&mem_fun_struct::u_mem_fun)(&ls, 10);
                //mem_fun(&mem_fun_struct::u_mem_fun)(&ls, 10, 20);
            }

            上面的代碼在dev-cpp 4.9.9中編譯通過,SGI STL中沒有提供二元成員函數(shù)的mem_fun,vc6中提供了mem_fun(無參數(shù)成員函數(shù))和mem_fun1(一元參數(shù)成員函數(shù)), 而在vs2003中改變了用法.但是我看MSDN好像也只支持到一個參數(shù).

            總結(jié)

            STL中提供了基本的一元和二元參數(shù)的Functor, 同時提供了相應(yīng)的適配器可以對Functor進行修飾,Functor可以很好的和 STL容器,STL算法結(jié)合使用.

            但是仍有問題:
            1)上面說到的調(diào)用方式
            2) 多參數(shù)函數(shù)對象適配

            對于我們比較復(fù)雜的stl不能滿足要求的問題,我們可以是用boost或loki來解決。


            參考

            本文基本是對hdqqq的文章轉(zhuǎn)載,稍加整理!原文地址:http://www.shnenglu.com/hdqqq/archive/2006/09/13/12424.aspx

            同時參考msdn:http://msdn2.microsoft.com/en-us/library/4y7z5x4b(VS.80).aspx
             

             

             

            posted on 2007-08-14 11:37 夢在天涯 閱讀(7320) 評論(2)  編輯 收藏 引用 所屬分類: CPlusPlus

            評論

            # re: STL函數(shù)對象及函數(shù)對象適配器[未登錄] 2007-08-14 13:32 flyman

            not1 //對一元的函數(shù)對象取反
            not2 //對二元的函數(shù)對象取反

            對對象取反好像不太恰當(dāng):

            函數(shù)對象的OPERATOR()返回值取反。


            不錯!繼續(xù)。  回復(fù)  更多評論   

            # re: STL函數(shù)對象及函數(shù)對象適配器 2008-06-06 14:46 bneliao

            講解很清晰  回復(fù)  更多評論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807624
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久99国产精品久久久| 欧美亚洲国产精品久久高清| 韩国无遮挡三级久久| 伊人色综合久久天天| 亚洲伊人久久综合影院| 日本人妻丰满熟妇久久久久久| 免费观看成人久久网免费观看| 日韩欧美亚洲国产精品字幕久久久 | 久久久久国产精品三级网| 日日狠狠久久偷偷色综合96蜜桃 | 亚洲国产精品无码久久久久久曰| 久久成人小视频| 久久最新精品国产| 欧美丰满熟妇BBB久久久| 久久久国产一区二区三区| 久久久久人妻一区精品色| 伊人久久大香线蕉无码麻豆| 狠狠色丁香久久婷婷综| 亚洲AV无码久久精品蜜桃| 久久免费大片| 久久久噜噜噜久久中文字幕色伊伊| 国产亚洲精久久久久久无码77777| 久久成人精品| 国产精品九九久久免费视频 | 亚洲精品高清国产一线久久| 久久精品成人一区二区三区| 美女写真久久影院| 国产精品99久久久久久人| 久久综合88熟人妻| 久久丫精品国产亚洲av不卡 | 久久久青草青青国产亚洲免观| 国产精品国色综合久久| 欧美熟妇另类久久久久久不卡 | 手机看片久久高清国产日韩| 久久国产视屏| 久久精品国产欧美日韩| 久久久久久国产a免费观看不卡| 88久久精品无码一区二区毛片| 久久精品国产只有精品2020| 久久精品国产免费一区| 国产精品美女久久久|