• <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++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            引領boost(六)(boost::bind)

             

            Boost::bind


            一 Boost::bind
               
               在STL中,我們經常需要使用bind1st,bind2st函數綁定器和fun_ptr,mem_fun等函數適配器,這些函數綁定器和函數適配器使用起來比較麻

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

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

            二 源碼剖析

             1) bind1st,bind2st函數綁定器,把二元函數對象變為一元函數對象。
             2) mem_fun,把成員函數變為函數對象。
             3) fun_ptr,把一般的全局函數變為函數對象。
             4) boost::bind(),包含了以上所有的功能。


            三 實例

             1)區別與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)區別與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)區別傳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)綁定虛函數

            //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) 現在的類庫最多可以支持9個參數。
             2)在綁定一個成員函數時,bind 表達式的第一個參數必須是成員函數所在類的實例!理解這個規則的最容易的方法是,這個顯式的參數將取

            替隱式的 this ,被傳遞給所有的非靜態成員函數。細心的讀者將會留意到,實際上這意味著對于成員函數的綁定器來說,只能支持八個參數

            ,因為第一個要用于傳遞實際的對象。
             3)當我們傳遞某種類型的實例給一個 bind 表達式時,它將被復制,除非我們顯式地告訴 bind 不要復制它。要避免復制,我們必須告訴

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

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

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

            五 參考

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

             

            posted on 2007-09-05 14:40 夢在天涯 閱讀(6634) 評論(4)  編輯 收藏 引用 所屬分類: CPlusPlusSTL/Boost

            評論

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

            在第一個sample代碼中2行注釋的代碼有問題,誰能幫忙解決哦,請高手指點哦!  回復  更多評論   

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

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

            //std::mem_fun1_ref<void,some_class,const std::stirng>(&some_class::print_string)(sc0,"hello2");
            如果你用的是Macrosoft的STL,std::mem_fun1_ref會有bug,即只能用于非const成員函數,如果把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");

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

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

            螞蟻終結者說的是正確的

            std::mem_fun_ref(&some_class::print_string)(sc0,"hello2");
            是正確的!(主要是我一開始以為它只能用于無參數函數的情況,看來是錯誤的,可以用于有一個參數的函數)

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

            不知道stl里有沒有提供更多參數的支持!  回復  更多評論   

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

            @夢在天涯
            有更多的參數支持。但默認只支持10個以內。
            需要更多的參數,要修改源代碼中的一個宏定義。我以前曾看過,代碼中有50個以內的參數定義預留。
              回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804159
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产福利电影一区二区三区久久老子无码午夜伦不 | 欧美色综合久久久久久| 久久精品国产免费一区| 99久久无码一区人妻a黑| 久久午夜伦鲁片免费无码| 久久中文字幕人妻丝袜| 精品国产青草久久久久福利| 尹人香蕉久久99天天拍| 中文成人无码精品久久久不卡| 中文字幕精品久久| 久久强奷乱码老熟女网站| 欧洲精品久久久av无码电影| 国产精品久久久久久久久鸭| 久久免费精品视频| 欧美精品丝袜久久久中文字幕| 久久99这里只有精品国产| 丁香色欲久久久久久综合网| 国产亚洲色婷婷久久99精品| 国产成人精品久久免费动漫 | 久久青草国产精品一区| 久久久无码精品亚洲日韩软件| 久久天天日天天操综合伊人av| 思思久久99热只有频精品66| 久久亚洲精精品中文字幕| 久久国产精品久久| 亚洲欧洲精品成人久久曰影片| 久久无码人妻一区二区三区 | 久久综合色之久久综合| 久久精品国产久精国产一老狼| 久久66热人妻偷产精品9| 国産精品久久久久久久| 伊人久久大香线蕉av不卡 | 麻豆亚洲AV永久无码精品久久| 亚洲一本综合久久| 中文字幕乱码人妻无码久久| 91久久成人免费| 久久精品无码专区免费青青 | 久久天天躁狠狠躁夜夜躁2O2O | 久久亚洲高清综合| 国产亚洲婷婷香蕉久久精品| 波多野结衣久久|