STL vector C++代码实现部分功能

#include <iostream>
#include <assert.h>
using namespace std;

template<typename T>
class my_iterator;
template <typename T>
class MyVector
{
public:
    typedef my_iterator<T> iterator;
    MyVector(int initsize = 0) :theSize(initsize), theCapcity(initsize + SPARE_CAPACITY)
    {
        array = new T[theCapcity];
    }

    ~MyVector()
    {
        theSize = 0;
        theCapcity = 0;
        if (array != NULL)
        {
            delete[]array;
            array = NULL;
        }
    }

    int capacity() const
    {
        return theCapcity;
    }

    void reserve(int newtheCapcity)
    {
        if (newtheCapcity < theSize)
        {
            return;
        }
        T* oldarray = array;
        array = new T[newtheCapcity];
        for (int i = 0; i < theSize; i++)
        {
            array[i] = oldarray[i];
        }
        theCapcity = newtheCapcity;
        delete[]oldarray;
        oldarray = NULL;
    }

    int size()const
    {
        return theSize;
    }

    void resize(int newthesize)
    {
        if (newthesize > theCapcity)
        {
            reserve(newthesize * 2 + 1);
        }
        theSize = newthesize;
    }

    T& operator[](int index)
    {
        return array[index];
    }

    bool empty()
    {
        return theSize == 0;
    }

    int move(int low, int high)//前移
    {
        if (low == theSize - 1)
            return 1;
        if (low == high)
        {
            return -1;
        }
        while (high < theSize)
        {
            array[low++] = array[high++];//前移
        }
        return 1;
    }
    int remove(int first, int end, int x)
    {
        int tmp = theSize - 1;
        int mxnumber = 0;
        for (int i = first; i <= end; )
        {
            if (array[i] == x)
            {
                mxnumber += move(i, i + 1);
                array[tmp] = x;
                tmp--;
                end--;
            }
            else
            {
                i++;
            }
        }
        return mxnumber;
    }

    void erase(int position)
    {
        for (int i = position; i < theSize; i++)
        {
            array[i] = array[i + 1];
        }
        theSize--;
    }
    void erase(int First, int Last)
    {
        assert(First > -1 && First < theSize);
        assert(Last > -1 && First < Last);
        if (First > Last)
        {
            int tmp = First;
            First = Last;
            Last = tmp;
        }
        if (First == Last)
        {
            erase(First);
        }
        else
        {
            while (Last < theSize)
            {
                array[First++] = array[Last++];
            }
            theSize -= (Last - First);
        }
    }

    void assign(const int n, const T& val)
    {
        theSize = 0;
        if (n > theCapcity)
        {
            reserve(n + 1);
        }
        for (int i = 0; i < n; i++)
        {
            array[i] = val;
            theSize++;
        }
    }

    void assign(const T* First, const T* Last)
    {
        int tmp = Last - First;
        theSize = 0;
        if (tmp > theCapcity)
        {
            reserve(tmp + 1);
        }
        for (int i = 0; i < tmp; i++)
        {
            array[i] = *First;
            theSize++;
            First++;
        }
    }

    T* data()
    {
        return array;
    }

    void push_back(const T& x)
    {
        emplace_back(x);
    }

    void emplace_back(const T& args)
    {
        if (theSize == theCapcity)
        {
            reserve(theCapcity * 2 + 1);
        }
        array[theSize++] = args;
    }

    iterator begin()
    {
        if (empty())
        {
            throw exception("此时的容器为空!");
        }
        return iterator(this, 0);
    }

    iterator end()
    {
        if (empty())
        {
            throw exception("此时的容器为空!");
        }
        return iterator(this, theSize);
    }

    enum { SPARE_CAPACITY = 16 };
private:
    T* array;
    int theSize;
    int theCapcity;
};

//迭代器代码
template<typename T>
class MyVector;
template<typename T>
class my_iterator
{
public:
    
    my_iterator(MyVector<T>* v, int ind)
        :vec(v), index(ind)
    {
    }

    bool operator!=(const my_iterator left)
    {
        return index != left.index;
    }

    my_iterator& operator++()
    {
        index++;
        return *this;
    }

    const my_iterator operator++(int)
    {
        my_iterator<T> tmp(*this);
        index++;
        return *this;
    }

    my_iterator& operator--()
    {
        index--;
        return *this;
    }
    const my_iterator operator--(int)
    {
        my_iterator<T> tmp(*this);
        index--;
        return *this;
    }

    const my_iterator operator+(int a)
    {
        my_iterator<T> tmp(*this);
        tmp.index += a;
        return tmp;
    }

    const my_iterator operator-(int a)
    {
        my_iterator<T> tmp(*this);
        tmp.index -= a;
        return tmp;
    }

    T& operator*();
    friend class MyVector<T>;
private:
    MyVector<T>* vec;
    int index;
};
template<typename T>
T& my_iterator<T>::operator*()
{
    return (*vec)[index];
}

int main()
{
    MyVector<int> arr;
    for (int i = 1; i <= 100; i++)
    {
        arr.push_back(i);
    }
    //arr.push_back(1);
    //arr.push_back(1);//
    //arr.push_back(6);
    //arr.push_back(1);
    //arr.push_back(1);
    //arr.push_back(1);
    //arr.push_back(1);//
    //arr.push_back(1);
    //arr.push_back(1);
    //arr.push_back(6);
    for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
    {
        cout << *its << " ";
    }
    cout << endl << arr.size() << endl;
    for (int i = 0; i <= 100; i++)
    {
        if (arr[i] % 5 == 0)
        {
            arr.erase(i);
        }
    }
    for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
    {
        cout << *its << " ";
    }
    cout << endl << arr.size() << endl;
//    remove
//    /*for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//    {
//        cout << *its << " ";
//    }
//    cout << endl << arr.size()<<endl;
//    cout<<arr.remove(0,arr.size()-1,1)<<endl;
//    for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//    {
//        cout << *its << " ";
//    }
//    cout << endl << arr.size() << endl;
//    cout << arr.remove(0, arr.size() - 1, 6) << endl;
//    for (MyVector<int>::iterator its = arr.begin(); its!=arr.end(); its++)
//    {
//        cout << *its<< " ";
//    }
//    cout << endl << arr.size() << endl;
//*/
//
//
//erase(int)
///*arr.erase(10);
//for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//{
//    cout << *its << " ";
//}
//cout << endl << arr.size() << endl;
//arr.erase(9);
//for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//{
//    cout << *its << " ";
//}
//cout << endl << arr.size() << endl;*/
//arr.erase(0);
//arr.erase(arr.size()); 
//arr.erase(-1);
//
//erase(first,end)
///*arr.erase(0,10);
//for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//{
//    cout << *its << " ";
//}
//cout << endl << arr.size() << endl;
//arr.erase(0, 10);
//for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//{
//    cout << *its << " ";
//}
//cout << endl << arr.size() << endl;*/
//
//assign(int,int)
///*arr.assign(7,1);
//for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//{
//    cout << *its << " ";
//}
//cout << endl << arr.size() << endl;
//arr.assign(120, 2);
//for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//{
//    cout << *its << " ";
//}
//cout << endl << arr.size() << endl;
//int brr[10] = { 1,2,3,4,56,765,6,45643,2,12343242 };
//arr.assign(brr,brr+10);
//for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//{
//    cout << *its << " ";
//}
//cout << endl << arr.size() << endl;
//arr.assign(brr, brr + 5);
//for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//{
//    cout << *its << " ";
//}
//cout << endl << arr.size() << endl;*/
//
//data
///*int *p = arr.data();
//*p = 10;
//++p;
//*p = 20;
//p[2] = 100;
//for (MyVector<int>::iterator its = arr.begin(); its != arr.end(); its++)
//{
//    cout << *its << " ";
//}
//cout << endl << arr.size() << endl;*/


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值