C++STL之自制vector


#include <algorithm>
#include <iostream>
#include <assert.h>

using namespace std;
#define WALK_LENGTH 64;

template <typename T>
class myVector
{
private:
    T *array;
    unsigned int theSize;
    unsigned int theCapacity;

    T *allocator(unsigned int size)
    {
        return new T[size];
    }

    void deallocator(T *arr)
    {
        if (arr)
        {
            delete[] arr;
        }
    }

public:
    // 构造函数
    myVector()
    {
        theCapacity = 0;
        theSize = 0;
        array = 0;
    }

    myVector(const T *arr, unsigned int n)
    {
        theCapacity = 0;
        theSize = 0;
        array = 0;
        for (int i = 0; i < n; i++)
        {
            push_back(arr[i]);
        }
    }

    // 复制内容
    myVector(const myVector<T> &other)
    {
        *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();
    }

    // 重构"[]"
    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;
    }

    // 清空myCector
    void clear()
    {
        deallocator(array);
        array = 0;
        theSize = 0;
        theCapacity = 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(unsigned int pos, const T &t)
    {
        insert_before(pos + 1, t);
    }

    // 添加到元素前
    void insert_before(unsigned int pos, const T &t)
    { // 如果元素数量等于容量时,容量进行扩容
        if (theSize == theCapacity)
        {
            T *oldArray = array;
            theCapacity += WALK_LENGTH;
            array = allocator(theCapacity);
            for (unsigned int i = 0; i < theSize; ++i)
            {
                array[i] = oldArray[i];
            }
            deallocator(oldArray);
        }

        // pos下标之后所有元素依次向后移动一位
        for (unsigned int i = theSize++; i > pos; --i)
        {
            array[i] = array[i - 1];
        }
        // 修改pos下标的元素为新元素
        array[pos] = t;
    }

    // 删除一个元素
    void earse(unsigned int pos)
    {
        if (pos < theSize)
        {
            --theSize;
            // pos下标后的所有元素向前移动一位
            for (unsigned int i = pos; i < theSize; ++i)
            {
                array[i] = array[i + 1];
            }
        }
    }

    // 打印vector
    void show()
    {
        cout << "[";
        for (unsigned int i = 0; i < theSize; i++)
        {

            if (i == theSize - 1)
            {
                cout << array[i];
            }
            else
            {
                cout << array[i] << ",";
            }
        }
        cout << "] alloc size = " << theCapacity << ", size = " << theSize << endl;
    }
};

void test()
{
    int arr[5] = {1, 2, 3, 4, 5};
    myVector<int> v1;
    myVector<int> v2(arr, 5);
    v1.push_back(0);
    v1.push_back(1);
    v1.push_front(-1);
    v2.push_back(1);
    v2.push_back(2);
    v2.push_back(3);
    v2.push_front(-1);
    v2.insert_before(2, 1);
    v2.insert_after(3, 4);
    v1.show();
    v2.show();
}

int main()
{
    test();
    return 0;
}