• <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>
            隨筆-91  評論-137  文章-0  trackbacks-0
            首先是vector的定義
                    template <typename T>
                    class vector
                    {
                    };

            讓我們先來看看vector中的一些別名
                    public:
                        typedef T         value_type;
                        typedef T*        pointer;
                        typedef T&        reference;
                        typedef const T&  const_reference;
                        typedef size_t    size_type;
                        typedef ptrdiff_t difference_type;
                        typedef const T* const_iterator;
                        typedef reverse_iterator<const_iterator, value_type, size_type, difference_type> const_reverse_iterator;
                        typedef T* iterator;
                        typedef reverse_iterator<iterator, value_type, size_type, difference_type> reverse_iterator;
            由上可見,正如上一篇所說,vector的迭代器是由原生的指針來實現的。

            下面是其內部的成員變量
                    protected:
                        typedef vector<T>    self;
                        typedef allocator<T> Alloc;

                        iterator start;
                        iterator finish;
                        iterator end_of_element;
            start:指向vector的起始地址
            finish:指向最后一個元素的后一個元素的地址
            end_of_element:指向已申請內存塊的結束位置(finish總是小于或等于end_of_element)

            在STL中從begin到end總是以一個前閉后開的形式來表示的,形如[begin,end),這樣做的好處是可以使代碼寫的更簡潔:
                    template <typename Iterator, typename T>
                    Iterator find(Iterator first, Iterator last, const T& value)
                    {
                        while(first != last && *first != value) ++first;
                        return first;
                    }

            下面來看看vector中最基本的構造函數和析構函數
                        vector() : start(0), finish(0), end_of_element(0)
                        {
                        }

                        ~vector()
                        {
                            destruct(start, end_of_element);
                            if (start != 0) Alloc::deallocate(start, end_of_element - start);
                        }
            這里將其中的3個成員變量都填充為0。
            上一篇所說,在STL中是將內存分配與對象初始化分開的,同樣對象析構與內存釋放也是被分開的。

            然后是其begin和end方法,用來獲取第一個元素和最后一個元素的后一個元素的迭代器。
                        inline iterator begin()
                        {
                            return start;
                        }

                        inline const_iterator begin()const
                        {
                            return start;
                        }

                        inline iterator end()
                        {
                            return finish;
                        }

                        inline const_iterator end()const
                        {
                            return finish;
                        }

            front和back分別被用來獲取第一個元素和最后一個元素
                        inline reference front()
                        {
                            return *begin();
                        }

                        inline const_reference front()const
                        {
                            return *begin();
                        }

                        inline reference back()
                        {
                            return *(end() - 1);
                        }

                        inline const_reference back()const
                        {
                            return *(end() - 1);
                        }

            empty、size、capacity分別被用來判別容器是否為空、容器內元素的個數和容器的大小
                        inline bool empty()const
                        {
                            return begin() == end();
                        }

                        inline const size_type size()const
                        {
                            return size_type(end() - begin());
                        }

                        inline const size_type capacity()const
                        {
                            return size_type(end_of_element - begin());
                        }

            由于在vector的頭部插入元素會使所有元素后移,應此它被設計為單向的,只能由尾部插入或移除數據
                        void push_back(const T& x)
                        {
                            if (end_of_element != finish)
                            {
                                construct(&*finish, x);
                                ++finish;
                            }
                            else
                            {
                                insert_aux(end(), x);
                            }
                        }

                        inline void pop_back()
                        {
                            --finish;
                            destruct<T>(finish, has_destruct(*finish));
                        }
            當然從頭部移除數據也并非不可以,可以使用erase方法來移除頭部的數據,erase方法將會在后面的部分作出說明。

            我們先來看一下insert_aux這個方法,在插入元素時幾乎都使用到了這個方法。
                        void insert_aux(const iterator position, const T& x)
                        {
                            if(finish != end_of_element)
                            {
                                construct(&*finish, *(finish - 1));
                                T x_copy = x;
                                copy_backward(position, finish - 1, finish);
                                *position = x_copy;
                                ++finish;
                            }
                            else
                            {
                                const size_type old_size = size();
                                const size_type new_size = old_size == 0 ? 2 : old_size * 2;
                                iterator tmp = Alloc::allocate(new_size);
                                uninitialized_copy(begin(), position, tmp);
                                iterator new_position = tmp + (position - begin());
                                construct(&*new_position, x);
                                uninitialized_copy(position, end(), new_position + 1);
                                destruct(begin(), end());
                                Alloc::deallocate(begin(), old_size);
                                end_of_element = tmp + new_size;
                                finish = tmp + old_size + 1;
                                start = tmp;
                            }
                        }
            在容器還有足夠的空間時,首先將從position位置到finish位置的元素整體后移一個位置,最后將要被插入的元素寫入到原position的位置同時改變finish指針的值。
            若空間不足時,首先根據原有空間的大小的一倍來申請內存,然后將元素從原有位置的begin到position拷貝到新申請的內存中,然后在新申請內存的指定位置插入要插入的元素值,最后將余下的部分也拷貝過來。然后將原有元素析構掉并把內存釋放掉。

            為何不使用reallocate?
            reallocate的本意并不是在原有內存的位置增加或減少內存,reallocate首先會試圖在原有的內存位置增加或減少內存,若失敗則會重新申請一塊新的內存并把原有的數據拷貝過去,這種操作本質上等價于重新申請一塊內存,應此這里使用的是allocate而并非reallocate。

            然后讓我們來看一下insert和erase方法
                        inline iterator insert(iterator position, const T& x)
                        {
                            const size_type pos = position - begin();
                            if(finish != end_of_element && position == end())
                            {
                                construct(&*finish, x);
                                ++finish;
                            }
                            else insert_aux(position, x);
                            return begin() + pos;
                        }

                        iterator erase(iterator position)
                        {
                            destruct(position, has_destruct(*position));
                            if (position + 1 != end())
                            {
                                copy(position + 1, end(), position);
                            }
                            --finish;
                            return position;
                        }
            若是要在最后插入一個元素且容器的剩余空間還足夠的話,直接將元素插入到finish的位置,并將finish指針后移一位即可。若容器空間不夠或不是插在最后一個的位置,則調用insert_aux重新分配內存或插入。
            刪除時首先析構掉原有元素,若被刪元素不是最后一個元素,則將后面的所有元素拷貝過來,最后將finish指針前移一個位置。

            最后讓我們來看一下其中重載的運算符
                        self& operator=(const self& x)
                        {
                            if(&x == thisreturn *this;
                            size_type const other_size = x.size();
                            if(other_size > capacity())
                            {
                                destruct(start, finish);
                                Alloc::deallocate(start, capacity());
                                start = Alloc::allocate(other_size);
                                finish = uninitialized_copy(x.begin(), x.end(), start);
                                end_of_element = start + other_size;
                            }
                            else
                            {
                                finish = uninitialized_copy(x.begin(), x.end(), start);
                            }
                            return *this;
                        }

                        inline reference operator[](size_type n)
                        {
                            return *(begin() + n);
                        }

                        inline value_type at(size_type n)
                        {
                            return *(begin() + n);
                        }
            由于vector內部用的是原生的指針,應此這些運算符的使用方式和原生指針的并無差異。值得注意的是在做賦值操作時會產生內存的重新分配與拷貝操作。

            至此,vector的講解已完成,完整的代碼請到http://qlanguage.codeplex.com下載
            posted on 2012-06-17 17:08 lwch 閱讀(3541) 評論(3)  編輯 收藏 引用 所屬分類: STL

            評論:
            # re: 山寨STL實現之vector[未登錄] 2012-06-18 11:22 | 路人甲
            “迭代器”比較麻煩,得做到自己版本的vector的迭代器能拿到std中使用。
              回復  更多評論
              
            # re: 山寨STL實現之vector 2012-06-26 00:58 | 朱峰everettjf
            學習,
            自己最近看了STL源碼剖析,總是在看,卻未想自己實現一個。
              回復  更多評論
              
            # re: 山寨STL實現之vector 2012-06-26 09:42 | lwch
            @朱峰everettjf
            程序還是要靠多寫才行~  回復  更多評論
              
            # re: 山寨STL實現之vector 2012-06-30 21:28 | beibei
            博主,我剛好也在做這個,但在實現含有兩個迭代器的構造函數時,出了問題:
            template<class In>
            Vec(In b, In e) {
            start=alloc.allocate( e-b );
            finish=end_of_element=std::uninitialized_copy( b, e, data );
            }
            在最后一行出現了error C2665。  回復  更多評論
              
            中文字幕精品久久| 欧洲人妻丰满av无码久久不卡| 成人午夜精品久久久久久久小说| 久久精品国产91久久综合麻豆自制| 99热都是精品久久久久久| 欧美一级久久久久久久大| 精品久久久无码21p发布| 精品久久777| 一级做a爰片久久毛片免费陪 | 久久国产香蕉视频| 人妻无码精品久久亚瑟影视| AAA级久久久精品无码片| 国产精品伦理久久久久久| 乱亲女H秽乱长久久久| 久久亚洲2019中文字幕| 久久国产精品成人影院| 无夜精品久久久久久| 亚洲国产精品久久久久网站| 亚洲中文久久精品无码| 久久久久久一区国产精品| 久久91综合国产91久久精品| 久久精品国产99久久久古代| 日韩亚洲欧美久久久www综合网 | 青草国产精品久久久久久| 欧美激情精品久久久久久久九九九| 麻豆AV一区二区三区久久| 午夜精品久久久久久影视riav| 国产成人久久777777| 成人国内精品久久久久一区| 久久狠狠爱亚洲综合影院| 久久久久黑人强伦姧人妻| 亚洲国产精品久久久久| 精品久久久久久中文字幕| 麻豆AV一区二区三区久久| 国产成人精品三上悠亚久久| 久久中文字幕精品| 成人综合久久精品色婷婷| 中文成人无码精品久久久不卡| 久久97久久97精品免视看| 亚洲国产成人久久综合一| 久久成人精品视频|