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

            統計

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

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 166403
            • 排名 - 159

            最新評論

            閱讀排行榜

            評論排行榜

            簡版 容器vector 實現

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

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

            }
            有了這些就足夠了,一下就是簡版Vector實現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            //創建新的存儲區域并賦值
            27            elmType* newElm=_alc.allocate(capacity);
            28            if (newElm==NULL)
            29            {
            30                return false;
            31            }

            32            uninitialized_copy(_first,_last,newElm);
            33            //刪除就有存儲區
            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}
            ;

            從實現看來,我覺得在使用vector,有以下幾點注意:
            1. 最好為所存儲對象實現拷貝構造函數,不然在重新分配存儲空間時可能會產生內存訪問違規問題,原因是對象在存入vector和在vecotr重新構造是都會調用拷貝構造函數
            2. 對象存入是會在制定位置產生新的對象,不必擔心原對象生存期

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

            99久久这里只精品国产免费| 精品久久久噜噜噜久久久| 亚洲精品tv久久久久久久久久| 色欲综合久久躁天天躁| 99久久婷婷国产综合亚洲| 久久亚洲中文字幕精品一区四| 久久狠狠爱亚洲综合影院| 久久久久久a亚洲欧洲aⅴ| 久久狠狠爱亚洲综合影院| 99久久精品免费看国产一区二区三区| 亚洲日韩欧美一区久久久久我| 国产精品久久久久久久久| 综合人妻久久一区二区精品| 久久中文精品无码中文字幕| 精品久久一区二区| 麻豆一区二区99久久久久| 色综合久久中文字幕综合网| 国产成人久久精品二区三区| 久久精品中文闷骚内射| 亚洲愉拍99热成人精品热久久| 久久久亚洲精品蜜桃臀| 精品久久久久久久久久中文字幕| 97久久精品人妻人人搡人人玩 | 久久国产精品国语对白| 精品少妇人妻av无码久久| 久久精品国产乱子伦| 国产精品美女久久福利网站| 久久精品国产清自在天天线| 久久99精品久久久久久噜噜| 中文字幕亚洲综合久久2| 久久免费小视频| 久久精品一区二区| 久久午夜电影网| 久久综合久久综合九色| 国产99久久久久久免费看| 青青草原综合久久大伊人精品| 99久久婷婷国产综合亚洲| 香蕉久久一区二区不卡无毒影院| 国产99久久久国产精免费| 久久久久99精品成人片三人毛片| 日本久久中文字幕|