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

            Boost::array


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

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

            。顯然對這個情況,上面的使用原始數組或STL容器都有點不太合適,因為原始的數組顯得笨重,的我們自己實現一些重復的功能,但是如果是

            使用STL容器的話,有可能會導致空間和性能的下降。

               基于上面的情況,所以在Boost中引入Boost::array,也可能會被加入下一代的C++標準中。Boost::array,內部仍然是固定長度,但是卻擁有

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


            二 源碼剖析  

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

             

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

             

            三 實例 

            #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在線document

             

            posted on 2007-08-24 17:49 夢在天涯 閱讀(4868) 評論(1)  編輯 收藏 引用 所屬分類: CPlusPlus

            評論

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

            寫的很好!!!
            樓主能不能加上Multi_Index的例子呢?對這個一直沒搞明白。  回復  更多評論   

            公告

            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

            搜索

            •  

            積分與排名

            • 積分 - 1807666
            • 排名 - 5

            最新評論

            閱讀排行榜

            波多野结衣中文字幕久久| 中文无码久久精品| 国产亚洲精品自在久久| 久久九九久精品国产免费直播| 狠狠精品久久久无码中文字幕 | 一本色道久久99一综合| 亚洲国产成人久久一区WWW| 久久亚洲电影| 18禁黄久久久AAA片| 久久亚洲精品国产亚洲老地址| 欧美久久天天综合香蕉伊| 亚洲国产成人精品91久久久| 色综合久久88色综合天天 | 一级做a爰片久久毛片毛片| 亚洲精品tv久久久久| 久久人人爽人人人人爽AV| 久久中文骚妇内射| 9999国产精品欧美久久久久久| 青青国产成人久久91网| 久久无码人妻精品一区二区三区| 亚洲国产成人久久综合野外| 亚洲中文字幕无码一久久区| 久久国产精品一国产精品金尊| 一本色道久久88加勒比—综合| 久久精品免费大片国产大片| 久久人人添人人爽添人人片牛牛 | 日韩欧美亚洲国产精品字幕久久久| 99久久综合国产精品二区| 久久99久久无码毛片一区二区| 亚洲а∨天堂久久精品9966| 无码人妻精品一区二区三区久久| 2021久久精品国产99国产精品| 国产精品青草久久久久福利99| 亚洲欧美精品一区久久中文字幕| 亚洲精品乱码久久久久久按摩| 久久99精品综合国产首页| 性做久久久久久久久老女人| www.久久精品| 久久这里只有精品18| 久久综合久久伊人| 777久久精品一区二区三区无码|