類比實現vector

來源:互聯網
上載者:User

vector.h

#pragma once#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>using namespace std;//vector的插入和刪除都存在迭代器失效的問題,解決方案將插入和刪除的函數//的參數給成引用,可以達到如下結果:當上述兩個函數內部迭代器失效,也會使得//外部的迭代器也同樣失效,那麼就要在函數內部將其重設//-------------------------------------------------------------//類型萃取struct __TrueType{    bool Get()    {        return true;    }};struct __FalseType{    bool Get()    {        return false;    }};template<class T>struct TypeTraits{    typedef __FalseType __IsPODType;};template<>struct TypeTraits<int>{    typedef __TrueType __IsPODType;};template<>struct TypeTraits <char>{    typedef __TrueType __IsPODType;};template<>struct TypeTraits<float>{    typedef __TrueType __IsPODType;};template<>struct TypeTraits<double>{    typedef __TrueType __IsPODType;};template<>struct  TypeTraits <long>{    typedef __TrueType __IsPODType;};template<class T>void __Copy(T*& dst, T* const & src, size_t size){    if (TypeTraits<T>::__IsPODType().Get())    {        memcpy(dst, src, size* sizeof(T));    }    else    {        for (size_t i = 0; i < size; ++i)        {            dst[i] = src[i];        }    }}//-----------------------------------------------------------------------template<class T>class Vector{public :    typedef T* Iterator;    typedef const T* ConstIterator;    //建構函式    Vector()        :_start(NULL)        , _finish(NULL)        , _endOfStorage(NULL)    {}    Vector(int n, const T& data = T())        :_start(NULL)        , _finish(NULL)        , _endOfStorage(NULL)    {        _start = new T[n];        for (size_t i = 0; i < n; ++i)        {            _start[i] = data;        }        _finish = _start + n;        _endOfStorage = _finish;    }    //拷貝建構函式    Vector(const Vector<T>& v)        :_start(0)        , _finish(0)        , _endOfStorage(0)    {        //可以分為兩種情況:①容量等於size(stl)  ②容量大於size        size_t size = v.Size();        _start = new T[size];        /*for (size_t i = 0; i < size; ++i)         {            _start[i] = v._start[i];        }*/        __Copy<T>(_start, v._start, size);        _finish = _start + v.Size();        _endOfStorage = _finish;    }    size_t Size()const    {        return _finish - _start;    }    size_t Capacity()const    {        return _endOfStorage - _start;    }    void PopBack()    {        if (_start)        {            --finish;        }    }    Iterator Begin()    {        return _start;    }    ConstIterator Begin()const    {        return _start;    }    Iterator End()    {        return _finish;    }    ConstIterator End()const    {        return _finish;    }    //增容    void CheckEndOfStorage()    {        size_t size = Size();        if (_finish == _endOfStorage)        {            //1.開空間 2.賦值 3.釋放舊空間 4.賦值            Iterator tmp = new T[2 * size + 3];            for (size_t i = 0; i < size; ++i)            {                tmp[i] = _start[i];            }            delete[] _start;            _start = tmp;            _finish = _start + size;            _endOfStorage = _start + 2 * size + 3;        }    }    //插入(存在迭代器失效問題)    void Insert(Iterator& pos,const T& value)    {        size_t size = pos - _start;        size_t oldSize = Size();        if (pos == End() && _finish < _endOfStorage)//容量足夠且尾插        {            _start[oldSize] = value;            ++_finish;        }        else//不是尾插        {            CheckEndOfStorage();            Iterator it = _finish;            pos = _start + size;//重設pos            while (pos != it)            {                *it = *(it - 1);                it--;            }            *pos = value;            ++_finish;        }    }    //將參數給成引用可以解決迭代器失效問題    Iterator Erase(Iterator& pos)    {        Iterator end = End();        Iterator cur = pos;        while (cur != end)        {            *cur = *(cur + 1);            cur++;        }        --_finish;        pos--;        return pos;    }    T& operator[](size_t index)    {        return *(_start + index)    }    const T& operator[](size_t index)const    {        return *(_start + index)    }    //capacity的值在vector中是以1/2來增長的    void Expand(size_t size)    {        size_t capacity = Capacity();        if (size == capacity)        {            capacity = capacity + capacity / 2;            if (capacity < Size() + 1)            {                capacity = Size() + 1;            }        }        _start = new T[capacity];        _finish = _start + size;        _endOfStorage = _start + capacity;    }    bool Empty()    {        return _start == _finish;    }    void Print()    {        size_t size = Size();        for (size_t i = 0; i < size; ++i)        {            cout << _start[i] << " ";        }        cout << endl;    }    //解構函式    ~Vector()    {        if (_start)        {            delete[] _start;            _start = NULL;            _finish = NULL;            _endOfStorage = NULL;        }    }protected :    Iterator _start;    Iterator _finish;    Iterator _endOfStorage;};

測試代碼如下:

#include"vector1.h"void Test(){    /*Vector<int> v1;    cout << v1.Size() << endl;*/    Vector<int > v2(10, 29);    cout << v2.Size() << endl;    Vector<int> v3(v2);    v3.Print();    cout << v3.Size() << endl;    /*Vector<int>::Iterator pos = v3.Begin();    v3.Insert(pos, 5);    v3.Print();    cout << v3.Size() << endl;*/    Vector<int>::Iterator pos = v3.Begin();    v3.Erase(pos);    v3.Print();    cout << v3.Size() << endl;}int main(){    Test();    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.