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

            統(tǒng)計

            • 隨筆 - 50
            • 文章 - 42
            • 評論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 165515
            • 排名 - 159

            最新評論

            閱讀排行榜

            評論排行榜

            簡版 容器vector 實現(xiàn)

            vector為我們提供了可伸縮的順序存儲容器,在順序和隨機存儲方面效率很高

            實現(xiàn)vector的關(guān)鍵在于實現(xiàn)內(nèi)存分配和對象構(gòu)造的分離,一般來講我們直接用new來構(gòu)造對象就經(jīng)歷了這兩個過程,而實現(xiàn)vector就需要我們先申請請到一片連續(xù)的內(nèi)存區(qū)域,然后在需要時在改內(nèi)存上構(gòu)造對象。這里用到allocator模板類
            該類內(nèi)部封裝了如下方法:
            template<typename elm>
            class allocator
            {
            elm *allocate(size_t n)      //分配n個對象存儲空間
            void construct(elm* p,size_t n) //在以p為開始的內(nèi)存上構(gòu)造n個對象
            void destroy(elm* p) //銷毀對象
            void deallocate(elm*p,size_t n) //釋放從p開始的n個對象內(nèi)存

            }
            有了這些就足夠了,一下就是簡版Vector實現(xiàn)LyVector
             1#include "stdafx.h"
             2#include "memory"
             3template <typename elmType>
             4class LyVector
             5{
             6private:
             7    elmType* _first;
             8    elmType* _last;
             9    elmType* _end;
            10    allocator<elmType> _alc;
            11public:
            12    typedef typename elmType valueType;
            13    LyVector():
            14      _first(0),_last(0),_end(0){};
            15    bool push_back(const elmType &t)
            16    {
            17        if (_last==_end)
            18        {
            19            size_t size=_last-_first;
            20
            21            size_t capacity=2*size;
            22            if (capacity==0)
            23            {
            24                capacity=2;
            25            }

            26            //創(chuàng)建新的存儲區(qū)域并賦值
            27            elmType* newElm=_alc.allocate(capacity);
            28            if (newElm==NULL)
            29            {
            30                return false;
            31            }

            32            uninitialized_copy(_first,_last,newElm);
            33            //刪除就有存儲區(qū)
            34            for(;_first!=_last;)
            35                _alc.destroy(--_last);
            36            _alc.deallocate(_first,size);
            37            _first=newElm;
            38            _last=newElm+size;
            39            _end=newElm+capacity;
            40        }

            41        _alc.construct(_last++,t);
            42        return true;
            43    }

            44    void popBack()
            45    {
            46        if (size()==0)
            47        {
            48            _DEBUG_ERROR("No element fount");
            49            return;
            50        }

            51        _alc.destroy(--_last);
            52    }

            53    
            54    size_t size()
            55    {
            56        return _last-_first;
            57    }

            58    size_t capacity()
            59    {
            60        return _end-_first;
            61    }

            62    
            63    elmType* operator[](size_t pos)
            64    {
            65        if (pos>=size())
            66        {
            67            _DEBUG_ERROR("out of range");
            68            return NULL;
            69        }

            70        return _first+pos;
            71    }

            72
            73
            74    friend ostream& operator<<(ostream &o,const LyVector &im)
            75    {
            76        for (LyVector::valueType * first=im._first;first!=im._last;first++)
            77        {
            78            o<<*first<<endl;
            79        }

            80        return o;
            81    }

            82}
            ;

            從實現(xiàn)看來,我覺得在使用vector,有以下幾點注意:
            1. 最好為所存儲對象實現(xiàn)拷貝構(gòu)造函數(shù),不然在重新分配存儲空間時可能會產(chǎn)生內(nèi)存訪問違規(guī)問題,原因是對象在存入vector和在vecotr重新構(gòu)造是都會調(diào)用拷貝構(gòu)造函數(shù)
            2. 對象存入是會在制定位置產(chǎn)生新的對象,不必擔(dān)心原對象生存期

            posted on 2009-08-12 15:46 pear_li 閱讀(2494) 評論(0)  編輯 收藏 引用 所屬分類: C++

            久久精品无码一区二区三区日韩 | 久久国产高潮流白浆免费观看| 久久嫩草影院免费看夜色| 国产精品成人久久久久久久| 久久国产精品波多野结衣AV| 欧美午夜精品久久久久久浪潮| 少妇人妻综合久久中文字幕| 亚洲欧美精品一区久久中文字幕 | 三级韩国一区久久二区综合 | 久久精品国产半推半就| 国产精品女同一区二区久久| 2021国产精品午夜久久| 91精品国产高清久久久久久io| 久久99精品久久久久久秒播 | 人妻系列无码专区久久五月天| 国产毛片欧美毛片久久久| 久久精品中文无码资源站| 久久久久18| 久久香蕉一级毛片| 色欲久久久天天天综合网精品| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久久久久亚洲AV无码专区| 久久93精品国产91久久综合| 久久婷婷五月综合色高清| 四虎国产精品成人免费久久| 婷婷综合久久中文字幕| 天堂久久天堂AV色综合| 久久久久亚洲av成人无码电影| 国产精品久久久久久久| 精品久久人人爽天天玩人人妻| 精品久久久久国产免费 | 精品久久久久久久中文字幕| 久久ww精品w免费人成| 久久久久久综合网天天| 亚洲欧美久久久久9999| 久久久网中文字幕| 精品久久久久久无码人妻蜜桃| 99久久国产综合精品五月天喷水| 久久精品国产亚洲77777| 久久精品水蜜桃av综合天堂| 久久久久久久久无码精品亚洲日韩 |