• <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++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            引領(lǐng)boost(六)(boost::bind)

             

            Boost::bind


            一 Boost::bind
               
               在STL中,我們經(jīng)常需要使用bind1st,bind2st函數(shù)綁定器和fun_ptr,mem_fun等函數(shù)適配器,這些函數(shù)綁定器和函數(shù)適配器使用起來(lái)比較麻

            煩,需要根據(jù)是全局函數(shù)還是類的成員函數(shù),是一個(gè)參數(shù)還是多個(gè)參數(shù)等做出不同的選擇,而且有些情況使用STL提供的不能滿足要求,所以如

            果可以我們最好使用boost提供的bind,它提供了統(tǒng)一的接口,提供了更多的支持,比如說(shuō)它增加了shared_ptr,虛函數(shù),類成員的綁定。

            二 源碼剖析

             1) bind1st,bind2st函數(shù)綁定器,把二元函數(shù)對(duì)象變?yōu)橐辉瘮?shù)對(duì)象。
             2) mem_fun,把成員函數(shù)變?yōu)楹瘮?shù)對(duì)象。
             3) fun_ptr,把一般的全局函數(shù)變?yōu)楹瘮?shù)對(duì)象。
             4) boost::bind(),包含了以上所有的功能。


            三 實(shí)例

             1)區(qū)別與mem_fun和fun_ptr

            #include <functional>
            #include 
            <iostream>
            #include 
            <string>
            #include 
            "boost/bind.hpp"
            class some_class 
            {
            public:      
                
            void print_string(const std::string& s) const
                
            {    
                    std::cout 
            << s << '\n'
                }

                
            void print_classname()
                
            {
                    std::cout 
            << "some_class" << std::endl;
                }

            }
            ;
            void print_string(const std::string s) 
            {  std::cout << s << '\n';
            }

            void print_functionname()
            {
                std::cout 
            << "Print_functionname" <<std::endl;
            }

            int main() 
            {  
                std::ptr_fun(
            &print_string)("hello1");
                
            //std::ptr_fun<void>(&print_functionname);
                some_class sc0;
                std::mem_fun_ref(
            &some_class::print_classname)(sc0);
                std::mem_fun_ref
            <void,some_class>(&some_class::print_classname)(sc0);
                
            //std::mem_fun1_ref<void,some_class,const std::stirng>(&some_class::print_string)(sc0,"hello2");

                (boost::bind(
            &print_string,_1))("Hello func!");  
                boost::bind(
            &print_functionname);
                some_class sc;  
                (boost::bind(
            &some_class::print_classname,_1)(sc));
                (boost::bind(
            &some_class::print_string,_1,_2))(sc,"Hello member!");
            }

             

             2)區(qū)別與bind1st和bind2st

            #include <functional>
            #include 
            <iostream>
            #include 
            <string>
            #include 
            <vector>
            #include 
            <algorithm>
            #include 
            "boost/bind.hpp"
            void main()
            {
                std::vector
            <int> ints;
                ints.push_back(
            7);
                ints.push_back(
            4);
                ints.push_back(
            12);
                ints.push_back(
            10);
                
            int count=std::count_if(ints.begin(),  
                    ints.end(), 
                    boost::bind(std::logical_and
            <bool>(),boost::bind(std::greater<int>(),_1,5),boost::bind(std::less_equal<int>(),_1,10))
                    );
                std::cout 
            << count << '\n';
                std::vector
            <int>::iterator int_it=std::find_if(ints.begin(),  
                    ints.end(),  
                    boost::bind(std::logical_and
            <bool>(),boost::bind(std::greater<int>(),_1,5),boost::bind(std::less_equal<int>(),_1,10))
                    );
                
            if (int_it!=ints.end()) 
                
            {  std::cout << *int_it << '\n';}

            }

             

             3)區(qū)別傳ref和傳instance

            // bind instance or reference
            #include <functional>
            #include 
            <iostream>
            #include 
            <string>
            #include 
            <vector>
            #include 
            <algorithm>
            #include 
            "boost/bind.hpp"
            class tracer 
            {
            public
                tracer() 
            {    std::cout << "tracer::tracer()\n";  } 
                tracer(
            const tracer& other) {    std::cout << "tracer::tracer(const tracer& other)\n";  } 
                tracer
            & operator=(const tracer& other)
                
            {    std::cout <<      "tracer& tracer::operator=(const tracer& other)\n";    return *this;  } 
                
            ~tracer() {    std::cout << "tracer::~tracer()\n"
                }
             
                
            void print(const std::string& s) const
                
            {    std::cout << s << '\n';  }
            }
            ;

            void main()
            {
                tracer t;
                boost::bind(
            &tracer::print,t,_1)(std::string("I'm called on a copy of t\n"));
                tracer t1;
                boost::bind(
            &tracer::print,boost::ref(t1),_1)(  std::string("I'm called directly on t\n"));

            }

             

             4)綁定虛函數(shù)

            //bind vitual class function
            #include <functional>
            #include 
            <iostream>
            #include 
            <string>
            #include 
            <vector>
            #include 
            <algorithm>
            #include 
            "boost/bind.hpp"
            class base {
            public:
                
            virtual void print() const 
                
            {    std::cout << "I am base.\n";  
                }
              
                
            virtual ~base() {}
            }
            ;
            class derived : public base 
            {
            public
                
            void print()const 
                
            {    std::cout << "I am derived.\n";  }
            }
            ;

            void main()
            {
                derived d;
                
            base b;
                boost::bind(
            &base::print,_1)(b);
                boost::bind(
            &base::print,_1)(d);
            }

             

             5)綁定成員變量

            // bind class's member
            #include <functional>
            #include 
            <iostream>
            #include 
            <string>
            #include 
            <vector>
            #include 
            <algorithm>
            #include 
            "boost/bind.hpp"
            class personal_info 

                std::
            string name_;  
                std::
            string surname_;
                unsigned 
            int age_;
            public
                personal_info(
            const std::string& n,const std::string& s,unsigned int age):name_(n),surname_(s),age_(age) {} 
                std::
            string name() const {return name_;} 
                std::
            string surname() const {return surname_;} 
                unsigned 
            int age() const {return age_;}
            }
            ;

            void main()
            {
             std::vector
            <personal_info> vec;
             vec.push_back(personal_info(
            "Little","John",30));
             vec.push_back(personal_info(
            "Friar""Tuck",50));
             vec.push_back(personal_info(
            "Robin""Hood",40));
             std::sort(vec.begin(),
                 vec.end(),
                 boost::bind(std::less
            <unsigned int>(),boost::bind(&personal_info::age,_1),boost::bind(&personal_info::age,_2))
                 );
             std::sort(vec.begin(),
                 vec.end(),
                 boost::bind(std::less
            <std::string>(),boost::bind(&personal_info::surname,_1),boost::bind(&personal_info::surname,_2))
                 );
            }

             

            四 注意

             1) 現(xiàn)在的類庫(kù)最多可以支持9個(gè)參數(shù)。
             2)在綁定一個(gè)成員函數(shù)時(shí),bind 表達(dá)式的第一個(gè)參數(shù)必須是成員函數(shù)所在類的實(shí)例!理解這個(gè)規(guī)則的最容易的方法是,這個(gè)顯式的參數(shù)將取

            替隱式的 this ,被傳遞給所有的非靜態(tài)成員函數(shù)。細(xì)心的讀者將會(huì)留意到,實(shí)際上這意味著對(duì)于成員函數(shù)的綁定器來(lái)說(shuō),只能支持八個(gè)參數(shù)

            ,因?yàn)榈谝粋€(gè)要用于傳遞實(shí)際的對(duì)象。
             3)當(dāng)我們傳遞某種類型的實(shí)例給一個(gè) bind 表達(dá)式時(shí),它將被復(fù)制,除非我們顯式地告訴 bind 不要復(fù)制它。要避免復(fù)制,我們必須告訴

            bind 我們想傳遞引用而不是它所假定的傳值。我們要用 boost::ref 和 boost::cref (分別用于引用和 const 引用)來(lái)做到這一點(diǎn),它們也是

            Boost.Bind 庫(kù)的一部分。還有一種避免復(fù)制的方法;就是通過(guò)指針來(lái)傳遞參數(shù)而不是通過(guò)值來(lái)傳遞。
             4) 通過(guò) Boost.Bind, 你可以象使用非虛擬函數(shù)一樣使用虛擬函數(shù),即把它綁定到最先聲明該成員函數(shù)為虛擬的基類的那個(gè)虛擬函數(shù)上。這

            個(gè)綁定器就可以用于所有的派生類。如果你綁定到其它派生類,你就限制了可以使用這個(gè)綁定器的類。
             5)bind還可以綁定成員變量。

            五 參考

            1)Beyond the C++ Standard Library: An Introduction to Boost
            2)boost在線document

             

            posted on 2007-09-05 14:40 夢(mèng)在天涯 閱讀(6653) 評(píng)論(4)  編輯 收藏 引用 所屬分類: CPlusPlus 、STL/Boost

            評(píng)論

            # re: 引領(lǐng)boost(六)(boost::bind) 2007-09-05 14:41 夢(mèng)在天涯

            在第一個(gè)sample代碼中2行注釋的代碼有問(wèn)題,誰(shuí)能幫忙解決哦,請(qǐng)高手指點(diǎn)哦!  回復(fù)  更多評(píng)論   

            # re: 引領(lǐng)boost(六)(boost::bind) 2007-09-05 16:52 螞蟻終結(jié)者

            //std::ptr_fun<void>(&print_functionname);
            std::ptr_fun返回的是unary function 或 binary function,不能用于無(wú)參函數(shù),實(shí)際上也沒必要。

            //std::mem_fun1_ref<void,some_class,const std::stirng>(&some_class::print_string)(sc0,"hello2");
            如果你用的是Macrosoft的STL,std::mem_fun1_ref會(huì)有bug,即只能用于非const成員函數(shù),如果把some_class中的
            void print_string(const string& s) const
            改為
            void print_string(const string& s)

            然后再這樣寫就可以了:
            std::mem_fun1_ref(&some_class::print_string)(sc0, "hello2");

            當(dāng)然了,用SGI的STL不會(huì)有這個(gè)bug
            最好的辦法是用std::mem_fun_ref,std::mem_fun_ref可用于一個(gè)參數(shù)或零個(gè)參數(shù)的const或non-const成員函數(shù),std::mem_fun1_ref只能用于一個(gè)參數(shù)的const或non-const成員函數(shù),估計(jì)是為了兼容性。
            所以也可以這樣寫:
            std::mem_fun_ref(&some_class::print_string)(sc0, "hello2");
            這樣print_string加不加const都一樣
              回復(fù)  更多評(píng)論   

            # re: 引領(lǐng)boost(六)(boost::bind) 2007-09-05 17:01 夢(mèng)在天涯

            螞蟻終結(jié)者說(shuō)的是正確的

            std::mem_fun_ref(&some_class::print_string)(sc0,"hello2");
            是正確的?。ㄖ饕俏乙婚_始以為它只能用于無(wú)參數(shù)函數(shù)的情況,看來(lái)是錯(cuò)誤的,可以用于有一個(gè)參數(shù)的函數(shù))

            //std::ptr_fun<void>(&print_functionname);
            這個(gè)可能也想螞蟻終結(jié)者說(shuō)的,沒有什么必要!

            不知道stl里有沒有提供更多參數(shù)的支持!  回復(fù)  更多評(píng)論   

            # re: 引領(lǐng)boost(六)(boost::bind) 2007-09-27 18:28 沐楓

            @夢(mèng)在天涯
            有更多的參數(shù)支持。但默認(rèn)只支持10個(gè)以內(nèi)。
            需要更多的參數(shù),要修改源代碼中的一個(gè)宏定義。我以前曾看過(guò),代碼中有50個(gè)以內(nèi)的參數(shù)定義預(yù)留。
              回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807518
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            精品国产91久久久久久久| 久久国产免费直播| 91久久精品电影| 日本精品久久久久影院日本 | 久久黄视频| 久久久久亚洲AV成人网人人网站 | 国产成人久久精品区一区二区| 亚洲午夜久久影院| 久久久久亚洲av成人网人人软件 | 88久久精品无码一区二区毛片| 人妻少妇精品久久| 久久国产成人精品麻豆| 中文字幕精品无码久久久久久3D日动漫 | 精品久久人人妻人人做精品 | 精品多毛少妇人妻AV免费久久 | 国产精品久久久久久久久软件| 久久国产精品一区二区| 大香伊人久久精品一区二区| 青青国产成人久久91网| 久久久亚洲欧洲日产国码aⅴ| 亚洲人成无码网站久久99热国产| 久久99国产精一区二区三区| 国产偷久久久精品专区| 国产精品久久婷婷六月丁香| 伊人久久精品影院| 思思久久99热免费精品6| 国产精品内射久久久久欢欢 | 久久只这里是精品66| 久久精品无码一区二区三区免费| 久久综合久久综合久久| 久久精品一区二区三区不卡| 91精品国产9l久久久久| 久久成人精品视频| 日本精品久久久久中文字幕| 国产精品99久久久久久人| 久久ww精品w免费人成| 久久精品国产亚洲av日韩| 午夜精品久久久久久久| 久久99精品久久久久久久不卡| 欧美黑人又粗又大久久久| 久久精品一本到99热免费|