手写vector实现

//vector类的简单实现
#include<algorithm>
#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
class myVector{
    private:
    //每次扩展的长度
    #define WALK_LENGTH 64;
    public:
    //默认构造函数
    myVector():array(0), theSize(0), theCapacity(0){}
    myVector(const T& t, unsigned int n):array(0), theSize(0), theCapacity(0){
        while(n--){
            push_back(t);
        }
    }
    //拷贝构造函数
    myVector(const myVector<T>& other): array(0), theSize(0), theCapacity(0){
        *this = other;
    }
    //重载=
    myVector<T>& operator = (myVector<T>& other){
        if(this == &other){
            return * this;
        }
        clear();
        theSize = other.size();
        theCapacity = other.capacity();
        array = new T[theCapacity];
        for(unsigned int i = 0;i < theSize; ++i){
            array[i] = other[i];
        }
        return *this;
    }
    ~myVector(){
        clear();
    }
    void clear(){
        deallocator(array);
        array = 0;
        theSize = 0;
        theCapacity = 0;
    }
    T& operator[](unsigned int pos){
        assert(pos<theSize);
        return array[pos];
    }
    unsigned int size(){
        return theSize;
    }
    unsigned int capacity(){
        return theCapacity;
    }
    bool empty(){
        return theSize == 0;
    }
    void push_back(const T& t){
        insert_after(theSize - 1, t);
    }
    void push_front(const T& t){
        insert_before(0, t);
    }
    void insert_after(int pos, const T& t){
        insert_before(pos+1, t);
    }
    void insert_before(int pos, const T& t){
        if(theSize == theCapacity){
            T* oldArray = array;
            theCapacity += WALK_LENGTH;
            array = allocator(theCapacity);
            //memcpy(array, oldArray, theSize * sizeof(T));
            for(unsigned int i = 0;i < theSize; ++i){
                array[i] = oldArray[i];
            }
            deallocator(oldArray);
        }
        for(int i = (int)theSize++; i > pos; --i){
            array[i] = array[i - 1];
        }
        array[pos] = t;
    }
    void erase(unsigned int pos){
        if(pos< theSize){
            --theSize;
            for(unsigned int i = pos; i < theSize; ++i){
                array[i] = array[i + 1];
            }
        }
    }
    private:
    T* allocator(unsigned int size){
        return new T[size];
    }
    void deallocator(T* arr){
        if(arr){
            delete[] arr;
        }
    }
    private:
    T* array;
    unsigned int theSize;
    unsigned int theCapacity;
};

void print(myVector<int>& vector1){
    for(unsigned int i = 0;i < vector1.size(); ++i){
        cout<<vector1[i]<<",";
    }
    cout<<"alloc size = "<<vector1.capacity() <<", size = "<<vector1.size() <<endl;
}
int main(){
    myVector<int> myVector1;
    myVector<int> myVector2(0, 10);
    myVector2.push_front(1);
    myVector2.erase(11);
    print(myVector2);
    myVector1.push_back(2);
    myVector1.push_back(1);
    print(myVector1);
    myVector1.insert_after(1, 3);
    print(myVector1);

    myVector2 = myVector1;
    myVector2.insert_before(0, 0);
    myVector2.insert_before(1, -1);
    print(myVector2);
    return 0;
}

输出

1,0,0,0,0,0,0,0,0,0,0,alloc size = 64, size = 11
2,1,alloc size = 64, size = 2
2,1,3,alloc size = 64, size = 3
0,-1,2,1,3,alloc size = 64, size = 5

由此可见vector的特点是:

1.随即访问元素效率很高;

2.push_back的效率很高;

3.push_front的效率很低,不建议使用;

4.insert需要把插入位置以后的元素全部后移,效率较低,不建议使用;

5.erase需要把删除位置后面的元素全部前移,效率较低,不建议使用;

6.当内存不够时,需要重新申请内存,再把以前的元素复制过来,效率比较低。

猜你喜欢

转载自blog.csdn.net/qq_24624539/article/details/108805822