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

            小明思考

            高性能服務(wù)器端計(jì)算
            posts - 70, comments - 428, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            boost庫的常用組件的使用

            Posted on 2005-11-23 09:39 小明 閱讀(10962) 評(píng)論(9)  編輯 收藏 引用 所屬分類: C/C++

            1.boost::any

            boost::any是一種通用的數(shù)據(jù)類型,可以將各種類型包裝后統(tǒng)一放入容器內(nèi)
            最重要的它是類型安全的。有點(diǎn)象COM里面的variant.

            使用方法:
            any::type() 返回包裝的類型
            any_cast可用于any到其他類型的轉(zhuǎn)化

             

            #include <boost/any.hpp>
            void test_any()
            {
             typedef std::vector
            <boost::any> many;
             many a;
             a.push_back(
            2);
             a.push_back(
            string("test"));

             
            for(unsigned int i=0;i<a.size();++i)
             
            {
              cout
            <<a[i].type().name()<<endl;
              
            try
              
            {
               
            int result = any_cast<int>(a[i]);
               cout
            <<result<<endl;
              }

              
            catch(boost::bad_any_cast & ex)
              
            {
               cout
            <<"cast error:"<<ex.what()<<endl;
              }

             }

            }


            2.boost::array

            boost::array僅僅是對(duì)數(shù)組一層薄薄的封裝,提供跟各種算法配合的iterator,使用方法很簡(jiǎn)單
            注意:可以使用{}來初始化array,因?yàn)閍rray所有的成員變量都是public的

             

            #include <boost/array.hpp>
            void test_array()
            {
             array
            <int,10> ai = {1,2,3};

             
            for(size_t i=0;i<ai.size();++i)
             
            {
              cout
            <<ai[i]<<endl;
             }

            }


            3.boost::lexical_cast
            lexical_cast用于將字符串轉(zhuǎn)換成各種數(shù)字類型(int,float,short etc.)

             

            #include <boost/lexical_cast.hpp>
            void test_lexical_cast()
            {
             
            int i = boost::lexical_cast<int>("123");
             cout 
            << i << endl;
            }


            4.boost::format
            boost::format是用于替代c里面的sprintf,優(yōu)點(diǎn)是類型安全,不會(huì)因?yàn)轭愋秃蛥?shù)不匹配而導(dǎo)致程序崩潰了
            而且還可以重復(fù)使用參數(shù)

             

            #include <boost/format.hpp>
            void test_format()
            {
             cout 
            << boost::format("writing %1%,  x=%2% : %3%-th try"% "toto" % 40.23 % 50 <<endl; 

             format f(
            "a=%1%,b=%2%,c=%3%,a=%1%");
             f 
            % "string" % 2 % 10.0;

             cout 
            << f.str() << endl;
            }


            5.boost::tokenizer
            boost::tokenizer是用于切割字符串的,類似于Java里面的StringTokenizer。

             

            #include <boost/tokenizer.hpp>
            void test_tokenizer()
            {
             
            string s("This is  , a ,test!");
             boost::tokenizer
            <> tok(s);
             
            for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
                   cout 
            << *beg << "\n";
             }

            }



            6.boost::thread
            boost::thread是為了提供跨平臺(tái)的thread機(jī)制。利用boost::function來完成委托。

             

            #include <boost/thread.hpp>
            void mythread()
            {
             cout
            <<"hello,thread!"<<endl;
            }


            void test_thread()
            {
             boost::function
            < void () > f(mythread);
             boost::thread t(f);
             t.join();
             cout
            <<"thread is over!"<<endl;
            }


            7.boost::serialization
            boost::serialization提供object的序列化功能。而且提供好幾種序列化的格式,比如text,binary,xml

             

            #include <boost/archive/text_oarchive.hpp>
            #include 
            <boost/archive/text_iarchive.hpp>
            #include 
            <boost/archive/xml_oarchive.hpp>
            void test_serialization()
            {
             boost::archive::text_oarchive to(cout , boost::archive::no_header);
             
            int i = 10;
             
            string s = "This is a test\n";
             to 
            & i;
             to 
            & s;

             ofstream f(
            "test.xml");
             boost::archive::xml_oarchive xo(f);
             xo 
            & BOOST_SERIALIZATION_NVP(i) & BOOST_SERIALIZATION_NVP(s);

             boost::archive::text_iarchive ti(cin , boost::archive::no_header);
             ti 
            & i & s;
             cout 
            <<"i="<< i << endl;
             cout 
            <<"s="<< s << endl;
            }

             

            8.boost::function
            boost::function就是所謂的泛函數(shù),能夠?qū)ζ胀ê瘮?shù)指針,成員函數(shù)指針,functor進(jìn)行委托,達(dá)到遲調(diào)用的效果

             

            #include <boost/function.hpp>
            int foo(int x,int y)
            {
             cout
            << "(foo invoking)x = "<<<< " y = "<< y <<endl;
             
            return x+y;
            }


            struct test
            {
             
            int foo(int x,int y)
             
            {
              cout
            << "(test::foo invoking)x = "<<<< " y = "<< y <<endl;
              
            return x+y;
             }

            }
            ;

            void test_function()
            {
             boost::function
            <int (int,int)> f;
             f 
            = foo;
             cout 
            << "f(2,3)="<<f(2,3)<<endl;

             test x;
             
            /*f = std::bind1st(
                  std::mem_fun(&test::foo), &x);
            */

             boost::function
            <int (test*,int,int)> f2;
             f2 
            = &test::foo;
              
             cout 
            << "f2(5,3)="<<f2(&x,5,3)<<endl;
            }


            9.boost::shared_ptr
            boost::shared_ptr就是智能指針的實(shí)現(xiàn),不象std::auto_ptr,它是可以stl的容器一起使用的,非常的方便

            #include <boost/shared_ptr.hpp>
            class Shared
            {
            public:
             Shared()
             
            {
              cout 
            << "ctor() called"<<endl;
             }

             Shared(
            const Shared & other)
             
            {
              cout 
            << "copy ctor() called"<<endl;
             }

             
            ~Shared()
             
            {
              cout 
            << "dtor() called"<<endl;
             }

             Shared 
            & operator = (const Shared & other)
             
            {
              cout 
            << "operator =  called"<<endl;
             }

            }
            ;

            void test_shared_ptr()
            {
             typedef boost::shared_ptr
            <Shared> SharedSP;
             typedef vector
            <SharedSP> VShared;
             VShared v;
             v.push_back(SharedSP(
            new Shared()));
             v.push_back(SharedSP(
            new Shared()));
            }

            Feedback

            # re: boost庫的常用組件的使用  回復(fù)  更多評(píng)論   

            2005-11-23 14:30 by atu123
            支持,繼續(xù)啊,最好對(duì)所講的內(nèi)容加以分類,形成一個(gè)系列,既對(duì)自己方便也便于閱讀,只是個(gè)人意見,僅供參考

            # re: boost庫的常用組件的使用  回復(fù)  更多評(píng)論   

            2005-11-23 18:55 by 可冰
            請(qǐng)問boost庫里有沒有XML的解析器,以及對(duì)Socket的封裝?

            # re: boost庫的常用組件的使用  回復(fù)  更多評(píng)論   

            2005-11-24 11:24 by 小明
            可惜.boost里面沒有對(duì)xml,socket的直接支持。database也沒有

            xml解析可以用xml4c
            socket最好用ACE,就是上手比較難
            database一種是使用ADO,另一種是廠家提供的API

            聽說boost::socket開發(fā)很長(zhǎng)時(shí)間了,可是沒成型
            boost::logging也還沒出來

            # re: boost庫的常用組件的使用  回復(fù)  更多評(píng)論   

            2006-01-19 00:39 by hygol
            還有boost::tuple和boost::regex之類的,也很好用。小明提到boost::serialization可以序列化XML文件的,能不能提供詳細(xì)一點(diǎn)的代碼?

            # re: boost庫的常用組件的使用  回復(fù)  更多評(píng)論   

            2006-02-10 11:39 by 黑風(fēng)
            不錯(cuò),幫我入門了。本來對(duì)boost一無所知的。

            # re: boost庫的常用組件的使用  回復(fù)  更多評(píng)論   

            2006-08-09 10:56 by 四海
            學(xué)習(xí),

            # re: boost庫的常用組件的使用  回復(fù)  更多評(píng)論   

            2007-03-21 16:00 by freebird
            socket相關(guān)的東西應(yīng)該使用更加權(quán)威的庫ACE

            # re: boost庫的常用組件的使用  回復(fù)  更多評(píng)論   

            2007-08-23 10:59 by 吳睿智
            謝謝
            我也入門了
            關(guān)注了滿久的boost

            # re: boost庫的常用組件的使用  回復(fù)  更多評(píng)論   

            2010-05-04 15:05 by stuarts
            樓主總結(jié)得很好,很強(qiáng)大,期待更多關(guān)于boost的內(nèi)容
            超级碰碰碰碰97久久久久| 久久国产视屏| 亚洲一区精品伊人久久伊人| 久久99免费视频| 精品少妇人妻av无码久久| 伊人久久大香线焦AV综合影院| 色欲综合久久躁天天躁| 国产视频久久| 久久99久久成人免费播放| 777久久精品一区二区三区无码| 久久久久亚洲av无码专区喷水| 亚洲欧美伊人久久综合一区二区| 中文字幕精品久久| 欧美黑人激情性久久| 亚洲愉拍99热成人精品热久久 | 久久久久青草线蕉综合超碰| 深夜久久AAAAA级毛片免费看| 久久综合九色欧美综合狠狠| 久久久久久国产精品无码下载 | 久久久久亚洲AV片无码下载蜜桃| 久久99热这里只有精品66| 伊人 久久 精品| 久久婷婷五月综合97色直播| 伊人久久大香线蕉AV色婷婷色| 亚洲国产另类久久久精品小说 | 久久精品国产一区| 国产69精品久久久久99| 亚洲欧美另类日本久久国产真实乱对白| 久久人人爽人人爽人人片AV东京热| 武侠古典久久婷婷狼人伊人| 午夜不卡久久精品无码免费| 久久久久国产精品| 天天影视色香欲综合久久| 看久久久久久a级毛片| 国产精品成人久久久久三级午夜电影| 久久精品一区二区三区中文字幕| 精品国产日韩久久亚洲| 国产精品久久久久天天影视| 人妻丰满?V无码久久不卡| 久久夜色精品国产噜噜麻豆 | 久久有码中文字幕|