• <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 逛奔的蝸牛 閱讀(357) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            国产成人久久精品区一区二区| 久久久久国色AV免费看图片| 久久久久亚洲AV无码永不| 久久永久免费人妻精品下载| 97久久精品人人做人人爽| 亚洲伊人久久成综合人影院 | www.久久热| 性高湖久久久久久久久AAAAA| 99久久免费国产精品特黄| 久久99热精品| 一本一道久久综合狠狠老| 精品久久久久久无码人妻蜜桃| 7777精品伊人久久久大香线蕉 | 久久九九精品99国产精品| 久久青青草原综合伊人| 亚洲色婷婷综合久久| 久久男人中文字幕资源站| 久久99国产精品久久99果冻传媒| 日韩欧美亚洲综合久久影院Ds | 亚洲色欲久久久综合网东京热| 久久亚洲欧洲国产综合| 热re99久久精品国产99热| 久久久久人妻精品一区二区三区 | 久久九九久精品国产| 日本福利片国产午夜久久| 久久精品国产亚洲AV高清热 | 久久久久久久久66精品片| 国产AⅤ精品一区二区三区久久| 久久青青草原亚洲av无码app| 久久受www免费人成_看片中文| 久久无码一区二区三区少妇 | 尹人香蕉久久99天天拍| 久久精品国产亚洲αv忘忧草| 久久久久无码精品国产不卡| 久久久久亚洲AV无码麻豆| 久久久亚洲欧洲日产国码aⅴ| 香蕉久久AⅤ一区二区三区| 人妻精品久久久久中文字幕| 精品久久久久久无码中文野结衣| 国产午夜福利精品久久| 91麻豆精品国产91久久久久久|