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

            [轉(zhuǎn)載]boost庫的使用介紹

            1.boost::any

            boost::any是一種通用的數(shù)據(jù)類型,可以將各種類型包裝后統(tǒng)一放入容器內(nèi)
            最重要的它是類型安全的。有點象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僅僅是對數(shù)組一層薄薄的封裝,提供跟各種算法配合的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用于將字符串轉(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)點是類型安全,不會因為類型和參數(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是為了提供跨平臺的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就是所謂的泛函數(shù),能夠?qū)ζ胀ê瘮?shù)指針,成員函數(shù)指針,functor進(jìn)行委托,達(dá)到遲調(diào)用的效果

            ?

            #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就是智能指針的實現(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()));
            }

            posted on 2006-10-05 10:55 永遇樂 閱讀(1078) 評論(0)  編輯 收藏 引用 所屬分類: STL & Boost

            <2008年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(6)

            隨筆分類

            推薦Blog

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            麻豆亚洲AV永久无码精品久久 | 久久精品国产亚洲AV大全| 久久人人爽人人爽人人片av高请 | 久久久久亚洲精品男人的天堂| 色8激情欧美成人久久综合电| 久久久久波多野结衣高潮| 国产一级持黄大片99久久| 综合久久给合久久狠狠狠97色| 国产精品一久久香蕉国产线看| 成人综合久久精品色婷婷| 91精品国产91久久久久久青草| 久久婷婷色综合一区二区| 国产精品无码久久久久| 久久夜色精品国产噜噜噜亚洲AV| 热久久国产欧美一区二区精品| 久久国产免费观看精品| 亚洲色大成网站www久久九 | 99久久精品免费国产大片| 久久久久久九九99精品| 亚洲午夜福利精品久久| 国产日韩久久久精品影院首页| 久久国产色AV免费观看| 亚洲精品乱码久久久久久中文字幕| 人人狠狠综合久久亚洲| 久久九九青青国产精品| 国产亚洲精品自在久久| 日韩精品久久无码中文字幕| 久久久久久免费视频| 一本久久精品一区二区| 欧美午夜精品久久久久久浪潮| 99热都是精品久久久久久| 日本福利片国产午夜久久| 国产日产久久高清欧美一区| 国产欧美久久一区二区| 久久国产精品一区二区| 7国产欧美日韩综合天堂中文久久久久 | 精品国产乱码久久久久久1区2区| 久久久亚洲裙底偷窥综合| 久久99热这里只有精品国产| 久久久久久久波多野结衣高潮| 久久这里都是精品|