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

            逛奔的蝸牛

            我不聰明,但我會(huì)很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::
            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 逛奔的蝸牛 閱讀(357) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C/C++
            久久久艹| 99久久99久久精品国产片| 亚洲AV日韩AV永久无码久久| 亚洲国产美女精品久久久久∴ | 亚洲欧洲精品成人久久曰影片| 青青草原综合久久大伊人导航| 中文精品99久久国产| 97精品依人久久久大香线蕉97 | 777米奇久久最新地址| 久久精品这里只有精99品| 久久综合狠狠综合久久| 色天使久久综合网天天| 青青热久久综合网伊人| 色婷婷久久综合中文久久蜜桃av| 久久婷婷人人澡人人| 久久久九九有精品国产| 亚洲精品乱码久久久久久自慰| 久久精品无码一区二区日韩AV| 午夜久久久久久禁播电影| 尹人香蕉久久99天天拍| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久最近最新中文字幕大全| 久久精品国产乱子伦| 四虎久久影院| 亚洲人成网站999久久久综合| 国产精品成人久久久久久久| 久久精品一区二区国产| 97超级碰碰碰久久久久| 韩国免费A级毛片久久| 久久久久久人妻无码| 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 久久精品国产一区二区三区| 国产亚州精品女人久久久久久| 99久久国产亚洲高清观看2024| 91精品国产91久久久久福利| 国产精品久久午夜夜伦鲁鲁| 精品免费久久久久久久| 国产一级做a爰片久久毛片| 久久免费精品一区二区| 久久99精品国产99久久6| 一本色道久久88综合日韩精品 |