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

c++ STL之vector的實現

概述

    vector作為STL中最常用的容器之一,他基於陣列實現,是一個動態陣列,其容量能自動增長,同時也提供了許多操作方法,十分高效實用。那麼我們來嘗試寫一個自己的vector吧!(當然,自己寫的肯定沒有STL中的高效,就當是一次學習探索吧)

開工!

如何實現動態陣列以及如何使其容量自動增長?

    這裡我們用指標通過在堆空間上開闢空間來實現動態陣列。設定兩個變數theSize(當前所含資料個數)和theCapacity(可容納資料最大數量),噹噹前數量即將超過最大容量時,重新分配空間。

vector中常用方法

template <typename Object> class Vector{
private :
    int theSize;
    int theCapacity;
    Object *objects;
public:
    typedef Object* iterator;//迭代器
    typedef const Object* const_iterator;
    explicit Vector(int initSize);
    Vector();
    Vector(const Vector<Object>&);
    ~Vector();
    const Vector<Object>&operator=(const Vector<Object>&);
    void push_back(const Object&);//向末尾新增
    void pop_back();//刪除最後一個元素
    const Object& back()const;//獲取最後一個元素
    const Object& font()const;//獲取第一個元素
    Object& back();
    Object& front();
    void resize(int);//重新設定容量
    void reserve(int);//重新設定最大容量
    const int size()const;
    const int capacity()const;
    bool empty();//是否為空
    void clear();//刪除所有元素
    Object& at(int);//獲取第幾個元素
    const Object& at(int)const;
    iterator begin(){return &objects[0];}
    iterator end(){return &objects[size()];}
    const_iterator begin()const{return &objects[0];}
    const_iterator end()const{return &objects[size()];}
    const Object&operator[](int)const;//過載下標運算子
    Object&operator[](int);
};

vector方法的實現

    實現都挺簡單的,值得注意的就是在重新分配空間的時候,記得刪除掉以前分配的空間,避免堆空間洩露(直接上程式碼了

template <typename Object> Vector<Object>::Vector():theSize(0),theCapacity(0),objects(NULL) {}
template <typename Object> Vector<Object>::Vector(int initSize) {
    this->theSize=initSize;
    this->theCapacity=this->theSize*2;
    this->objects=new Object[this->theCapacity];
}
template <typename Object> Vector<Object>::Vector(const Vector<Object> &rhs):objects(NULL) {
    operator=(rhs);
}
template <typename Object>Vector<Object>::~Vector() {
    if(objects)delete[]objects;
}
template <typename Object> const Vector<Object>& Vector<Object>::operator=(const Vector<Object> &rhs) {
    if(this!=&rhs){
        theSize=rhs.theSize;
        theCapacity=rhs.theSize;
        if(objects)
            delete[]objects;
        objects=new Object[theCapacity];
        for(int i=0;i<theSize;i++)
            objects[i]=rhs.objects[i];
    }
    return *this;
}
template <typename Object> void Vector<Object>::push_back(const Object &rhs) {
    if(theSize==theCapacity){
        if(theCapacity==0) {
            theCapacity = 1;
            objects =new Object[theCapacity];
        }
        else {
            theCapacity *= 2;
            Object *oldArray = objects;
            objects = new Object[theCapacity];
            for (int i = 0; i < theSize; i++)
                objects[i] = oldArray[i];
            delete[]oldArray;
        }
    }
    this->objects[theSize++]=rhs;
}
template <typename Object> void Vector<Object>::pop_back() {
    this->theSize--;
}
template <typename Object> const int Vector<Object>::size() {
    return this->theSize;
}
template <typename Object> const int Vector<Object>::capacity() {
    return this->theCapacity;
}
template <typename Object> bool Vector<Object>::empty() {
    return theSize==0?true:false;
}
template <typename Object> void Vector<Object>::clear() {
    if(!empty()){
        this->theSize=0;
        this->theCapacity=0;
        delete[]objects;
    }
}
template <typename Object> const Object& Vector<Object>::font()const {
    if(!empty())
        return objects[0];
}
template <typename Object> const Object& Vector<Object>::back()const {
    if(!empty())
        return objects[theSize-1];
}
template <typename Object> void Vector<Object>::resize(int newSize){
    if(newSize>theSize)
        for(int i=theSize;i<newSize;i++)
            objects=Object();
    this->theSize=newSize;
}
template <typename Object> void Vector<Object>::reserve(int newCapacity) {
    if(newCapacity<theCapacity)
        return;
    this->theCapacity=newCapacity;
    Object*oldArray=objects;
    objects=new Object[theCapacity];
    for(int i=0;i<theSize;i++)
        objects[i]=oldArray[i];
    delete[]oldArray;
}
template <typename Object> Object& Vector<Object>::at(int index) {
    if(index>=theSize||index<0)
        throw "Out of Range";
    return objects[index];
}
template <typename Object>const Object& Vector<Object>::at(int index)const {
    if(index>=theSize||index<0)
        throw "out of range";
    return objects[index];
}
template <typename Object>const Object& Vector<Object>::operator[](int index)const {
    if(index>=theSize||theSize<0)
        throw "out of range";
    return objects[index];
}
template <typename Object>Object& Vector<Object>::operator[](int index) {
    if(index>=theSize||theSize<0)
        throw "out of range";
    return objects[index];
}

最後

    一個vector差不多就這樣實現了,如果有能改進的地方,希望大家不吝賜教