容器(Vector)
容器是这样的一种数据存储上的【对象类型】,该对象可以持有其他对象或者其他对象的指针。
Vector的一些接口
我们可以通过【C++Reference】来搜索一下容器
我们发现容器里有很多的接口
这次我们实现以下接口
template<typename T>
class Vector
{
public:
Vector();
Vector(const T arr[], size_t size);
Vector(const Vector& v);
Vector& operator=(const Vector& s);
~Vector();
public:
void PushBack(const T& data);
void PopBack();
void Insert(size_t pos, const T& data);
void Erase(size_t pos);
const T& operator[](size_t index)const;
T& operator[](size_t index);
T& Front();
const T& Front()const;
T& Back();
const T& Back()const;
bool Empty()const;
size_t Size()const;
size_t Capacity()const;
void Clear();
void Resize(size_t size);
private:
void _CheckCapacity();
private:
T* _pData;
size_t _capacity;
size_t _size;
};
Vector的实现
template<typename T>
class Vector
{
friend ostream& operator<<(ostream &_cout,const Vector<T> &t)
{
for (size_t idx = 0; idx < t._size; ++idx)
{
_cout << t._pData[idx]<<" ";
}
return _cout;
}
public:
Vector()
:_pData(new T[3])
, _capacity(3)
, _size(0)
{}
Vector(const T arr[], size_t size)
:_pData(new T[3])
, _size(0)
, _capacity(3)
{
for (size_t idx = 0; idx < size; ++idx)
{
CheckCapacity();
_pData[idx] = arr[idx];
++_size;
}
}
Vector(const Vector<T>& t)
:_pData(new T[sizeof(T)*(t._capacity)])
, _capacity(t._capacity)
, _size(t._size)
{
for (size_t idx = 0; idx < t._size; ++idx)
{
_pData[idx] = t._pData[idx];
}
}
Vector<T>& operator=(const Vector<T> &t)
{
if (this != &t)
{
T* Temp = new T[t._capacity];
for (size_t idx = 0; idx < t._size; ++idx)
{
Temp[idx] = t._pData[idx];
}
delete[] _pData;
_pData = Temp;
_size = t._size;
_capacity = t._capacity;
}
return *this;
}
~Vector()
{
if (NULL != _pData)
{
delete[] _pData;
}
_pData = NULL;
_size = 0;
_capacity = 0;
}
void PushBack(const T& data)
{
CheckCapacity();
_pData[_size] = data;
++_size;
}
void PopBack()
{
if (_size > 0)
{
--_size;
}
}
void Insert(size_t pos, const T& data)
{
if (pos > _size)
{
return;
}
CheckCapacity();
size_t idx = _size;
for (; idx > pos; --idx)
{
_pData[idx - 1] = _pData[idx];
}
_pData[idx] = data;
++_size;
}
void Erase(size_t pos)
{
if (pos > _size)
{
return;
}
for (size_t idx = 0; idx < _size; ++idx)
{
_pData[idx] = _pData[idx + 1];
}
--_size;
}
T& Front()
{
return _pData[0];
}
const T& Front()const
{
return _pData[0];
}
T& Back()
{
return _pData[_size - 1];
}
const T& Back()const
{
return _pData[_size - 1];
}
const T& operator[](size_t index)const
{
return _pData[index];
}
//ture 为空
//false 不为空
bool Empty()const
{
if (0 == _size)
{
return ture;
}
return false;
}
size_t Size()
{
return _size;
}
size_t Size()const
{
return _size;
}
size_t Capacity()
{
return _capacity;
}
size_t Capacity()const
{
return _capacity;
}
void Clear()
{
_size = 0;
}
void Resize(size_t size,T &data = 0)
{
if (size < _size)
{
_size = size;
}
for (size_t idx = _size; idx < size; ++idx)
{
_pData[idx] = data;
}
}
private:
T* _pData;
size_t _capacity;
size_t _size;
void CheckCapacity()
{
while (_size >= _capacity)
{
T* Temp = new T[2*_capacity];
for (size_t idx = 0; idx < _size; ++idx)
{
Temp[idx] = _pData[idx];
}
delete[] _pData;
_pData = Temp;
_capacity *= 2;
}
}
};