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

            搜索

            •  

            積分與排名

            • 積分 - 165513
            • 排名 - 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)生新的對象,不必擔心原對象生存期

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

            国产呻吟久久久久久久92| 久久综合成人网| 久久精品aⅴ无码中文字字幕不卡| 亚洲午夜精品久久久久久浪潮| 超级97碰碰碰碰久久久久最新| 国内精品九九久久精品| 国产∨亚洲V天堂无码久久久| 狠狠色综合网站久久久久久久| 久久精品国产99国产精品澳门| 夜夜亚洲天天久久| 久久综合久久综合亚洲| 无码伊人66久久大杳蕉网站谷歌 | 97久久国产露脸精品国产| 一本一道久久综合狠狠老| 99精品久久精品| 中文字幕人妻色偷偷久久| 国产精品久久影院| 久久中文字幕人妻熟av女| 久久香蕉国产线看观看乱码| 久久国产精品无码HDAV| 久久成人精品| 亚洲精品高清久久| 精品综合久久久久久888蜜芽| 亚州日韩精品专区久久久| 青青青国产成人久久111网站| 婷婷综合久久中文字幕蜜桃三电影| 99久久国产亚洲高清观看2024| 久久久久久精品免费看SSS| 久久久精品波多野结衣| 国产精品9999久久久久| 奇米影视7777久久精品| 久久久久高潮毛片免费全部播放| 久久青青草原精品国产软件| 国产成人香蕉久久久久| 国产叼嘿久久精品久久| 久久美女网站免费| 77777亚洲午夜久久多喷| 人妻精品久久无码区| 欧美噜噜久久久XXX| 久久久久久久久无码精品亚洲日韩 | 久久久久久久久久免免费精品|