• <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::array)

            Boost::array


            一 Boost::array
               
               在以前,如果我們要處理一組數(shù)據(jù),我們可能使用一般數(shù)組存儲(chǔ),或者需要許多的對(duì)數(shù)組的數(shù)據(jù)的操作的時(shí)候,我們使用STL容器存儲(chǔ)。但

            是如果我們的需求是,我們能夠提前固定一組數(shù)據(jù)的大小,或提前知道這組數(shù)據(jù)的大小,但是我們又想這組數(shù)據(jù)進(jìn)行一些操作,比如排序。。

            。顯然對(duì)這個(gè)情況,上面的使用原始數(shù)組或STL容器都有點(diǎn)不太合適,因?yàn)樵嫉臄?shù)組顯得笨重,的我們自己實(shí)現(xiàn)一些重復(fù)的功能,但是如果是

            使用STL容器的話(huà),有可能會(huì)導(dǎo)致空間和性能的下降。

               基于上面的情況,所以在Boost中引入Boost::array,也可能會(huì)被加入下一代的C++標(biāo)準(zhǔn)中。Boost::array,內(nèi)部仍然是固定長(zhǎng)度,但是卻擁有

            向STL容器一樣的接口,這樣就使的Boost::array能夠支持STL中的算法,省去了很多的重復(fù)的工作,提高開(kāi)發(fā)效率。


            二 源碼剖析  

             template<class Ty, size_t N>
              
            class array {
            public:
              typedef size_t size_type;
              typedef ptrdiff_t difference_type;
              typedef Ty
            & reference;
              typedef 
            const Ty& const_reference;
              typedef Ty 
            *pointer;
              typedef 
            const Ty *const_pointer;
              typedef T0 iterator;
              typedef T1 const_iterator;
              typedef Ty value_type;
              typedef reverse_iterator
            <iterator>reverse_iterator;
              typedef reverse_iterator
            <const_iterator>
                const_reverse_iterator;

              
            void assign(const Ty& val);
              
            void swap(array& right);

              iterator begin();
              const_iterator begin() 
            const;
              iterator end();
              const_iterator end() 
            const;
              reverse_iterator rbegin();
              const_reverse_iterator rbegin() 
            const;
              reverse_iterator rend();
              const_reverse_iterator rend() 
            const;

              size_type size() 
            const;
              size_type max_size() 
            const;
              
            bool empty() const;

              reference 
            operator[](size_type off);
              const_reference 
            operator[](size_type off) const;
              reference at(size_type off);
              const_reference at(size_type off) 
            const;

              reference front();
              const_reference front() 
            const;
              reference back();
              const_reference back() 
            const;

              T 
            *data();
              
            const T *data() const;
              }
            ;


            template
            <class Ty, size_t N>
              
            class array;
                
            // FUNCTION TEMPLATES
            template<class Ty, size_t N>
              
            bool operator==(
                
            const array<Ty, N>& left,
                
            const array<Ty, N>& right);
            template
            <class Ty, size_t N>
              
            bool operator!=(
                
            const array<Ty, N>& left,
                
            const array<Ty, N>& right);
            template
            <class Ty, size_t N>
              
            bool operator<(
                
            const array<Ty, N>& left,
                
            const array<Ty, N>& right);
            template
            <class Ty, size_t N>
              
            bool operator<=(
                
            const array<Ty, N>& left,
                
            const array<Ty, N>& right);
            template
            <class Ty, size_t N>
              
            bool operator>(
                
            const array<Ty, N>& left,
                
            const array<Ty, N>& right);
            template
            <class Ty, size_t N>
              
            bool operator>=(
                
            const array<Ty, N>& left,
                
            const array<Ty, N>& right);
            template
            <class Ty, size_t N>
              
            void swap(
                array
            <Ty, N>& left,
                array
            <Ty, N>& right);

                
            // tuple-LIKE INTERFACE
            template<int Idx, class Ty, size_t N>
              Ty
            & get(array<Ty, N>& arr);
            template
            <int Idx, class Ty, size_t N>
              
            const Ty& get(const array<Ty, N>& arr);
            template
            <class Ty, size_t N>
              
            class tuple_element<array<Ty, N> >;
            template
            <class Ty, size_t N>
              
            class tuple_size<array<Ty, N> >;

             

            比較簡(jiǎn)單,只要用過(guò)STL容器的,都能明白,最后的幾個(gè)get,tuple_element,是在tr1中才有的,為了和tuple接口兼容。

             

            三 實(shí)例 

            #include <algorithm>
            #include 
            <iostream>
            #include 
            <stdexcept>

            #include 
            "boost/array.hpp"

            const int ELEMS = 6;

            template 
            <class Container>
            void do_sort (Container& values)

                std::sort (values.begin(), values.end());
            }



            int main()

                boost::array
            <int, ELEMS> values1 = 314298 };
                boost::array
            <int, ELEMS> values2 = {2,2,2};
                boost::array
            <int, ELEMS> values3(values1);
                boost::array
            <int, ELEMS> values4 = values2;

                boost::array
            <int, ELEMS>::size_type  num = values1.size();
                boost::array
            <int, ELEMS>::size_type maxnum = values1.max_size();
                
            bool isEmpty = values1.empty();

                
            // test for get function
                boost::array<int10> squares = 014916,2536496481 };
                
            int idx = -1;
                
            for (;;)
                
            {
                    
            try 
                        std::cout 
            << idx << " squared is "
                            
            << squares .at(idx) << '\n';
                        
            break;
                    }

                    
            catch(std::exception&)
                    

                        std::cout 
            << "Value to square: ";
                        std::cin 
            >> idx;
                    }

                }

                
            int ninesquare = squares[9];

                
            // test for iterator
                boost::array<int, ELEMS> values = 314298 };
                std::copy(values.begin(), values.end(),
                    std::ostream_iterator
            <int>(std::cout, " "));
                std::cout 
            << '\n';
                std::sort(values.rbegin(), values.rend());
                std::copy(values.begin(), values.end(),
                    std::ostream_iterator
            <int>(std::cout, " "));
                std::cout 
            << '\n';

                
            // test assign and swap
                boost::array<int,5> myarr1 = {1,2,3,4,5};
                boost::array
            <int,10> myarr2;
                myarr2.assign(
            10);
                boost::array
            <int,12> myarr3;
                
            //myarr3.swap(myarr1); // error
                boost::array<int,5> myarr;
                myarr.swap(myarr1);

                
            int* my1 = myarr1.c_array();
                
            int* my2 = myarr1.data();

                
            return 0;
            }

             

            四 注意

             

            五 參考

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

             

            posted on 2007-08-24 17:49 夢(mèng)在天涯 閱讀(4874) 評(píng)論(1)  編輯 收藏 引用 所屬分類(lèi): CPlusPlus

            評(píng)論

            # re: 引領(lǐng)Boost(五)(Boost::array) 2007-12-15 22:15 飛行器

            寫(xiě)的很好!!!
            樓主能不能加上Multi_Index的例子呢?對(duì)這個(gè)一直沒(méi)搞明白。  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

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

            常用鏈接

            隨筆分類(lèi)

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1811117
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            亚洲AV伊人久久青青草原| 成人国内精品久久久久影院VR| 91精品国产91久久久久久| 品成人欧美大片久久国产欧美...| 色综合久久88色综合天天 | 国产亚洲精久久久久久无码AV| 久久久久无码国产精品不卡| 亚洲精品无码久久千人斩| 久久99国产精品久久| 亚洲va国产va天堂va久久| 国内精品久久久久久久涩爱| 麻豆成人久久精品二区三区免费| 精品少妇人妻av无码久久| 国产精品久久久香蕉| 精品人妻伦九区久久AAA片69 | 久久精品免费观看| 99久久国产综合精品女同图片| 国内精品久久久久久不卡影院| 69久久精品无码一区二区| 久久久www免费人成精品| 午夜肉伦伦影院久久精品免费看国产一区二区三区| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久精品国产91久久麻豆自制 | 99久久精品午夜一区二区| 一本大道久久东京热无码AV| 久久天天躁狠狠躁夜夜2020| 久久久久99精品成人片三人毛片| 精品久久久久久无码中文字幕| 久久男人AV资源网站| 亚洲欧美国产精品专区久久| 热久久视久久精品18| 国产精自产拍久久久久久蜜| 国产精品免费久久久久久久久| 久久久久久久久波多野高潮| 欧美综合天天夜夜久久| 香蕉久久夜色精品国产2020| 久久精品嫩草影院| 国产亚洲精久久久久久无码77777 国产亚洲精品久久久久秋霞 | 亚洲精品第一综合99久久| 777久久精品一区二区三区无码 | 99久久国产精品免费一区二区|