• <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)計(jì)

            • 隨筆 - 50
            • 文章 - 42
            • 評(píng)論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 166405
            • 排名 - 159

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            簡(jiǎn)版 容器vector 實(shí)現(xiàn)

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

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

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

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

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

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

            国产精品伦理久久久久久| 日韩久久久久中文字幕人妻| 精品国产99久久久久久麻豆| 久久久久人妻一区二区三区| 亚洲中文字幕无码久久精品1 | 国产激情久久久久影院| 婷婷综合久久中文字幕| 91久久精品国产91性色也| 久久有码中文字幕| 久久精品国产精品亚洲毛片| 久久国产美女免费观看精品 | 99re这里只有精品热久久| 久久99热狠狠色精品一区| 精品久久久久久无码中文字幕| 狠狠色丁香久久婷婷综合| 欧美精品丝袜久久久中文字幕| 久久久久亚洲av无码专区喷水| 国产精品欧美久久久久天天影视| 久久国产V一级毛多内射| 久久人人青草97香蕉| 久久青青草原精品国产| 狠狠色丁香久久婷婷综合蜜芽五月| 99久久精品免费看国产一区二区三区| 久久不射电影网| 青青青国产精品国产精品久久久久| 一本色道久久88—综合亚洲精品| 人妻少妇久久中文字幕一区二区| 久久精品亚洲男人的天堂| 久久国产免费直播| 日韩美女18网站久久精品| 久久久中文字幕日本| 久久人人爽人爽人人爽av| 久久亚洲高清综合| 国产精品久久波多野结衣| 国产精品99久久不卡| 日日狠狠久久偷偷色综合0| 久久青青草原国产精品免费| 新狼窝色AV性久久久久久| 亚洲精品美女久久久久99小说| 99久久精品免费| 777午夜精品久久av蜜臀|