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

            牽著老婆滿街逛

            嚴以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            boost庫的常用組件的使用(ZT)

            1.boost::any

            boost::any是一種通用的數據類型,可以將各種類型包裝后統一放入容器內
            最重要的它是類型安全的。有點象COM里面的variant.

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

            ?

            #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僅僅是對數組一層薄薄的封裝,提供跟各種算法配合的iterator,使用方法很簡單
            注意:可以使用{}來初始化array,因為array所有的成員變量都是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用于將字符串轉換成各種數字類型(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,優點是類型安全,不會因為類型和參數不匹配而導致程序崩潰了
            而且還可以重復使用參數

            ?

            #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是為了提供跨平臺的thread機制。利用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就是所謂的泛函數,能夠對普通函數指針,成員函數指針,functor進行委托,達到遲調用的效果

            ?

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


            struct ?test
            {
            ?
            int ?foo( int ?x, int ?y)
            ?
            {
            ??cout
            << ? " (test::foo?invoking)x?=? " << 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就是智能指針的實現,不象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()));
            }
            ?

            posted on 2007-03-20 22:51 楊粼波 閱讀(342) 評論(0)  編輯 收藏 引用

            91久久成人免费| 久久99精品久久久久婷婷| 精品熟女少妇AV免费久久| 久久天堂AV综合合色蜜桃网| 国产欧美一区二区久久| 少妇被又大又粗又爽毛片久久黑人| 色天使久久综合网天天| 久久精品国产亚洲AV高清热| 久久一区二区三区99| 国产精品99久久99久久久| 国内精品伊人久久久久妇| 亚洲天堂久久精品| 日韩精品久久久肉伦网站 | 久久久久亚洲AV成人片| 久久国产成人亚洲精品影院 | 99久久免费国产精品| 77777亚洲午夜久久多喷| 免费精品久久久久久中文字幕| 72种姿势欧美久久久久大黄蕉| 国产欧美久久久精品影院| 国产精品综合久久第一页| 激情伊人五月天久久综合| 97精品伊人久久久大香线蕉| 婷婷久久综合九色综合绿巨人| 国产2021久久精品| 亚洲国产精品久久久久| 久久66热人妻偷产精品9| 亚洲精品无码久久千人斩| 久久久久亚洲AV无码观看| 久久青青草视频| 三级三级久久三级久久| 婷婷久久综合九色综合绿巨人| 国产—久久香蕉国产线看观看| 精品久久久久久久| 久久精品国产99国产精偷| 久久夜色精品国产亚洲| 久久香蕉一级毛片| 精品久久久无码中文字幕| 久久久久久A亚洲欧洲AV冫| 久久精品无码免费不卡| 日韩中文久久|