• <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 調(diào)用標(biāo)準(zhǔn)copy算法的特殊版本. 這個(gè)版本希望目的地是未構(gòu)造的原始內(nèi)存,它在目的地復(fù)制構(gòu)造每個(gè)元素, 而不是將輸入范圍的元素賦值給目的地(復(fù)制時(shí),左操作數(shù)是已經(jīng)存在,但在無默認(rèn)構(gòu)造函數(shù)的情況下肯定不行,所以用的是復(fù)制構(gòu)造).
            for循環(huán)對舊數(shù)組中每個(gè)對象調(diào)用allocator的destroy()函數(shù)運(yùn)行 T 類型的虛構(gòu)函數(shù)來釋放舊元素所用的任何資源.
            一旦復(fù)制和撤銷了舊元素,就釋放原來數(shù)組所用空間. 在調(diào)用deallocate之前,必須檢查elements是否實(shí)際指向一個(gè)數(shù)組.


            posted on 2008-03-15 02:35 逛奔的蝸牛 閱讀(352) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            久久午夜福利电影| 久久婷婷五月综合色99啪ak| 久久综合久久综合久久| 无码国内精品久久人妻蜜桃| 成人综合伊人五月婷久久| 久久久久久午夜成人影院| 日产精品久久久久久久性色| 久久久久免费视频| 久久人人添人人爽添人人片牛牛| 亚洲一区中文字幕久久| 久久久久久亚洲精品成人| 久久婷婷人人澡人人爽人人爱| 国产成人无码精品久久久久免费| 久久久久人妻精品一区| 久久AAAA片一区二区| 99久久免费国产特黄| 91精品国产色综久久 | 国内精品久久久久久久影视麻豆| 久久久久久九九99精品| 日韩中文久久| 久久久久亚洲AV片无码下载蜜桃| 精品视频久久久久| 亚洲午夜久久久久久久久电影网| 久久久无码人妻精品无码| 青青青青久久精品国产| 久久无码高潮喷水| 狠狠色丁香久久婷婷综| 久久久久人妻精品一区二区三区| 国产69精品久久久久9999APGF| 久久中文字幕视频、最近更新| 99久久精品免费看国产免费| 亚洲国产美女精品久久久久∴| 狠狠色丁香婷婷综合久久来来去 | 国产毛片久久久久久国产毛片| 久久久久青草线蕉综合超碰 | 久久久久久免费视频| 亚洲欧美日韩精品久久亚洲区 | 国产精品久久久久久久久久影院| 亚洲精品WWW久久久久久 | 精品久久久久中文字| 亚洲精品乱码久久久久久按摩 |