MyVector

#pragma once
/*  
T*buff;                         //数组名
unsigned int size;              //容量
unsigned int len;               //数组长度
*/
template<class T>
class MyVector
{   size_t size;//容量
    size_t len;//数组长度
    T*buff;//数组名
public:
    MyVector();

    ~MyVector()    { _clear();}

    //迭代器
    class MyIterator 
    {
        T*it;
    public:
        MyIterator() { it = NULL; }

        MyIterator(MyIterator&temp) { this->it = temp.buff; }

        ~MyIterator(){ it = NULL; }

        void operator=(const T*p)//赋值
        { it = (T*)p; }

        bool operator!=(T*temp)//不等于
        { return (it != temp); }

        T& operator*()const//解引用
        { return *it; }

        MyIterator& operator++()//前加加 
        { it++; return *this; }

        MyIterator& operator--()//前减减 
        { it--; return *this; }

        MyIterator& operator++(int) //后加加
        {
            MyIterator *temp = new MyIterator;
            temp->it = it;  this->it++;     
            return *temp;
        }

        MyIterator& operator--(int) //后减减
        {
            MyIterator *temp = new MyIterator;
            temp->it = it;  this->it--;
            return *temp;
        }
    };
    //MyItretor结束******************************

    T& operator[](int i)const               //下标
    { return buff[i]; }
    T* begin()const                         //返回数组首地址
    { return buff; }
    T* end()const                           //返回数组末尾地址
    { return (buff + len); }
    size_t getLen()                         //返回数组长度
    { return len; }
    size_t getSize()                        //返回数组大小
    { return size; }
    void _alloc();                          //开内存
    MyVector& _cp(const MyVector&);         //拷贝
    void push_back(const T& data);          //插入数据
    void _clear();                          //清空所有数据
    T& at(int);                             //代检查的下标
    bool empty();                           //判断容器是否为空,如果为空返回true   
    void swap(MyVector<T>&);                //交换两个容器
    void swap(size_t, size_t);              //交换一个数组中的两个元素
    MyVector& operator+=(const MyVector&);  //数组连接
};

template<class T>
MyVector<T>::MyVector()
{
    buff = NULL;
    size = len = 0;
}
//开内存
template<class T>
void MyVector<T>::_alloc()
{
    if (len >= size)
    {
        size = size + ((size >> 1) > 1 ? size >> 1 : 1);
        buff = (T*)realloc(buff, sizeof(T)*size);
    }
}
//插入数据
template<class T>
void MyVector<T>::push_back(const T& data)
{
    _alloc();
    buff[len++] = data; 
}
//清空所有数据
template<class T>
void MyVector<T>::_clear()
{
    if (buff)
        free(buff);
    size = len = 0;
    buff = NULL;
}
//代检查的下标
template<class T>
T& MyVector<T>::at(int i)
{
    if (i < 0 || i >= len)
        printf("\a越界了!\n");
    return buff[i];
}
//判断容器是否为空,如果为空返回true
template<class T>
bool MyVector<T>::empty()   
{
    if (buff)
        return 0;
    return 1;
}
//两个容器间的拷贝
template<class T>
MyVector<T>& MyVector<T>::_cp(const MyVector<T>&temp)
{
    _clear();
    size = len = temp.len;
    _alloc();
    for (int i = 0; i < len; i++)
        this->buff[i] = temp.buff[i];
    return *this;
}
//交换两个容器
template<class T>
void MyVector<T>::swap(MyVector<T>& tempVector)
{
    MyVector temp;
    temp._cp(*this);
    this->_cp(tempVector);
    tempVector._cp(temp);
    temp._clear();
}
//交换一个数组中的两个元素
template<class T>
void MyVector<T>::swap(size_t subscript, size_t subscript2)
{
    T temp = buff[subscript];
    buff[subscript] = buff[subscript2];
    buff[subscript2] = temp;
}
//数组连接
template<class T>
MyVector<T>&MyVector<T>::operator+=(const MyVector<T>&temp)
{
    size_t tempLen = len;
    size = len += temp.len;
    _alloc();
    for (size_t i = tempLen, j = 0; i < len; i++, j++)
        buff[i] = temp.buff[j];
    return *this;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值