自己实现vector

#ifndef MY_VECTOR_H
#define MY_VECTOE_H
#include<cassert>
typedef unsigned int  size_t;
template < class T>
class Vector {
public:
    typedef T * iterator;
    
    Vector();
    Vector(int size, T const& a);
    Vector(const Vector<T> & a);
    ~Vector();
    size_t size() const;
    size_t capacity() const;
    size_t max_capacity() const;
    void push_back(const T& val);
    void pop_back();
    T& operator[](int index);
    Vector<T>& operator=(const Vector<T>& a);
    bool empty() const;
    iterator begin() const;
    iterator end() const;
private:
    size_t  _size;
    size_t  _capacity;
    T*      _buf;
    const size_t _max_capacity = 65536;
};
template<class T>
Vector<T>::Vector()
{   
    _size = 0;
    _buf = new T[1];
    _capacity = 1;
}

template<class T>
Vector<T>::Vector(int s, const T& a)
{   
    if (s > _max_capacity) {
        s = _max_capacity;
    }
    _size = s;
    _capacity = 1;
    while (_capacity < _size) {
        _capacity *= 2;
    }
    _buf = new T[_capacity];
    for (size_t i = 0; i < _size; i++) {
        _buf[i] = a;
    }
}
template<class T>
Vector<T>::Vector(const Vector<T> & a)
{
    _size = a._size;
    _capacity = a._capacity;
    _buf = new T[_capacity];
    for (size_t i = 0; i < _size; i++) {
        _buf[i] = a._buf[i];
    }
}
template<class T>
Vector<T>::~Vector()
{
    delete[] _buf;
}

template<class T>
size_t Vector<T>::size() const
{
    return _size;
}

template<class T>
size_t Vector<T>::capacity() const
{
    return _capacity;
}
template<class T>
size_t Vector<T>::max_capacity() const
{
    return _max_capacity;
}
template<class T>
T& Vector<T>::operator[](int index)
{
    assert(index >= 0 && index < _size);
    return _buf[index];
}

template<class T>
void Vector<T>::push_back(const T& val)
{
    if (_size < _capacity) {
        _buf[_size] = val;
        _size++;
        return ;
    } else if (_size == _max_capacity) {
        return ;
    }
    _capacity *= 2;
    if (_capacity >= _max_capacity) {
        _capacity = _max_capacity;
    }
    T * tmp = new T[_capacity];
    for (size_t i = 0; i < _size; i++) {
        tmp[i] = _buf[i];
    }
    tmp[_size] = val;
    _size++; 
    delete[] _buf;
    _buf = tmp; 
}

template<class T>
void Vector<T>::pop_back()
{
    assert(_size > 0);
    _size--;
}
template<class T>
bool Vector<T>::empty() const
{
    if (_size == 0) {
        return true;
    }
    return false;
}
 // 迭代器的实现
template<class T>
typename Vector<T>::iterator Vector<T>::begin() const
{
    return _buf;
}
template<class T>
typename Vector<T>::iterator Vector<T>::end() const
{
    return _buf + _size;
}
template<class T>
Vector<T>& Vector<T>::operator=(const Vector<T> & a)
{
    if (this == &a) {
        return *this ;
    }
    delete[] _buf;
    _size = a._size;
    _capacity = a._capacity;
    _buf = new T[_capacity];
    for (size_t i = 0; i < _size; i++) {
        _buf[i] = a._buf[i];
    }
    return *this;
}
#endif

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值