1. 程式人生 > >c++ STL vector 的簡易實現

c++ STL vector 的簡易實現

template <class T>
class myVector
{
public:
    myVector():p(NULL),capacity(0),size(0)//預設建構函式
    {
    }
    myVector(int size)//預設賦值為0的建構函式
    {
        this->size=size;
        this->capacity=size;
        this->p=new T[capacity];
        for(int i=0;i<this->size;i++)
            this->p[i]=0;
    }
    myVector(int size,int data)//提供初值的建構函式
    {
        this->size=size;
        this->capacity=size;
        this->p=new T[capacity];
        for(int i=0;i<this->size;i++)
            this->p[i]=data;
    }
    //還可以提供其他一些用於初始化的建構函式

    //解構函式,釋放掉唯一的指標
    ~myVector()
    {
        if(p!=NULL)
            delete[] p;
    }
    //拷貝建構函式
    myVector(const myVector &v)
    {
        this->capacity=v.capacity;
        this->size=v.size;
        this->p=new T[this->capacity];//分配記憶體空間
        memcpy(this->p,v.p,this->size* sizeof(T));
    }
    //遍歷容器元素
    void print()
    {
        for(int i=0;i<this->size;i++)
            cout<<this->p[i]<<" ";
        cout<<endl;
    }
    //插入
    void push_back(T data)
    {
        if(this->p==NULL)
        {
            this->capacity=1;
            this->size=0;
            this->p=new T[capacity];
        }
        if(this->size==this->capacity)//擴容,重新分配記憶體空間
        {
            T *new_p=new T[this->capacity*2];
            memcpy(new_p,p,this->size* sizeof(T));
            this->capacity*=2;
            delete[]p;
            p=new_p;
        }
        this->p[this->size++]=data;
    }
    //刪除
    void pop_back()
    {
        if(this->size>1)
        {
            this->p[this->size-1]=0;
            this->size--;
        }
        error();
    }

    //指定位置刪除(元素後移)
    void insert(int pos,T data)
    {
        if(pos>=0&&pos<=this->size)
        {
            if(this->size==this->capacity)//已經滿了,擴容
            {
                T *new_p=new T[this->capacity*2];
                memcpy(new_p,this->p,this->size* sizeof(T));
                this->capacity*=2;
                delete[]p;
                p=new_p;
            }
            for(int i=this->size;i>pos;i--)
                this->p[size]=this->p[size-1];
            this->p[pos]=data;
            this->size++;
        }
        error();
    }
    void error()
    {
       cout<<"陣列越界!";
    }
    void clear()
    {
        this->size=0;
    }
    //過載[]運算子
    T &operator[](int index)
    {
        if(index>=0&&index<this->size)
            return this->p[index];
        error();
    }
    //過載=運算子
    T &operator=(const myVector &v)
    {
        if(this->p!=NULL)
        {
            delete []p;
            this->capacity=0;
            this->size=0;
            this->p=NULL;
        }
        this->capacity=v.capacity;
        this->size=v.size;
        this->p=new T[this->capacity];
        memcpy(p,v.p,this->size* sizeof(T));
    }
    int Size()
    {
        return this->size;
    }
    int Capacity()
    {
        return this->capacity;
    }

private:
    T *p;
    int capacity;
    int size;
};

//測試函式
void test()
{
    myVector<int> myVector1(3,1);
    myVector1.print();
    cout<<myVector1.Size()<<"--"<<myVector1.Capacity()<<endl;
    for(int iter = 0;iter<11;iter++)
    {
        myVector1.push_back(iter);
        myVector1.print();
        cout<<"size:"<<myVector1.Size()<<" capacity:"<<myVector1.Capacity()<<endl;
    }
    myVector1.pop_back();
    myVector1.pop_back();
    myVector1.pop_back();
    myVector1.print();
    myVector1[3]=10086;
    myVector1.print();

    myVector<int > myVector2(3,1);
    myVector2.print();
    myVector2.insert(0,4);
    myVector2.insert(1,222);
    myVector2.insert(myVector2.Size(),333);
    myVector2.print();
}
int main()
{
    test();
    return 0;
}

vector函式 底層通過capacity 和size變數以及一個T型別的指標 動態的實現 陣列的動態增長,通過過載[]運算子提供了隨機訪問,每次插入新的元素,會檢查當前的size是否和capacity相等,如果相等,則重新開闢一塊2*capacity大小的記憶體,並使用memcpy函式將內容原來的資料拷貝過去,還可以繼續新增更多的函式來豐富該類的功能。