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

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::
            template<typename T> class Vector {
            public:
                    Vector() :
                            elements(
            0), first_free(0), end(0) {
                    }

                    
            void push_back(const T &);
                    
            //.

            private:
                    
            static std::allocator<T> alloc; // Object to get raw memory.
                    void reallocate(); // Get more space and copy existing elements.
                    T *elements; // Pointer to first element in the array.
                    T *first_free; // Pointer to first free element in the array(no structed).
                    T *end; // Pointer to one past the end of the array.
            };

            template
            <typename T> void Vector<T>::push_back(const T &t) {
                    
            // Are we out of range ?
                    if (first_free == end)
                            
            // Get more space and copy the existing elements to it
                            reallocate();
                    alloc.construct(first_free, t);
                    
            ++first_free;
            }

            template
            <typename T> void Vector<T>::reallocate() {
                    
            // Compute size of current array and
                    
            // allocate space for twice as many elements
                    std::ptrdiff_t size = first_free - elements;
                    std::ptrdiff_t newcapacity 
            = 2 * max(size, 1);
                    
            // Allocate space to hold newcapacity number of elements of type T
                    T *newelements = alloc.allocate(newcapacity);

                    
            // Construct copies of the existing elements in new space
                    uninitialized_copy(elements, first_free, newelements);
                    
            // Destroy the old elements in reverse order
                    for (T *= first_free; p != elements;) {
                            alloc.destroy(
            --p);
                    }

                    
            // deallocate cannot be called on a 0 pointer
                    if (elements) {
                            alloc.deallocate(elements, end 
            - elements);
                    }

                    
            // Make our data structure pointer to the new elements
                    elements = newelements;
                    first_free 
            = elements + size;
                    end 
            = elements + newcapacity;
            }


            uninitialized_copy 調用標準copy算法的特殊版本. 這個版本希望目的地是未構造的原始內存,它在目的地復制構造每個元素, 而不是將輸入范圍的元素賦值給目的地(復制時,左操作數是已經存在,但在無默認構造函數的情況下肯定不行,所以用的是復制構造).
            for循環對舊數組中每個對象調用allocator的destroy()函數運行 T 類型的虛構函數來釋放舊元素所用的任何資源.
            一旦復制和撤銷了舊元素,就釋放原來數組所用空間. 在調用deallocate之前,必須檢查elements是否實際指向一個數組.


            posted on 2008-03-15 02:35 逛奔的蝸牛 閱讀(356) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            久久久综合香蕉尹人综合网| 亚洲精品成人网久久久久久| 粉嫩小泬无遮挡久久久久久| 蜜桃麻豆WWW久久囤产精品| 色天使久久综合网天天| 欧美精品久久久久久久自慰| 久久九九青青国产精品| 精品视频久久久久| 青青青国产精品国产精品久久久久| 久久精品国产一区| 伊人久久大香线蕉综合5g| 日产精品久久久久久久| 国产精品亚洲综合专区片高清久久久 | 久久无码AV一区二区三区| 久久久久久午夜成人影院| 国产亚洲精久久久久久无码AV| 久久这里有精品| 99热热久久这里只有精品68| 亚洲婷婷国产精品电影人久久| 国产精品久久久久久久久鸭| 思思久久精品在热线热| 久久人搡人人玩人妻精品首页 | 亚洲国产成人久久综合碰碰动漫3d| 午夜精品久久久久久影视777| 狠狠色丁香婷综合久久| 一级a性色生活片久久无| 精品久久久久久久久久中文字幕 | 亚洲午夜久久久久妓女影院 | 国产美女久久精品香蕉69| 亚洲国产日韩欧美综合久久| 91精品国产高清久久久久久91| 99久久99久久精品免费看蜜桃| 伊人久久大香线蕉亚洲五月天| 国产精品久久久久久久午夜片| 久久99国产亚洲高清观看首页| 浪潮AV色综合久久天堂| 久久综合给合久久国产免费| 久久精品亚洲中文字幕无码麻豆| 一本色综合网久久| 久久99精品久久久久久久不卡| 99久久婷婷国产综合亚洲|