#pragma once
#include <iostream>
using namespace std;
#define RESIZE 5
template<class T>
class Vector
{
public:
typedef T ValueType;
typedef ValueType* Pointer;
typedef const ValueType* ConstPointer;
typedef ValueType* Iterator;
typedef const ValueType* ConstIterator;
typedef ValueType& Reference;
typedef const ValueType& ConstReference;
typedef size_t SizeType;
public:
Vector()
: _start(0)
, _end(0)
, _endOfStorage(0)
{}
Vector(SizeType n, const T& value)
{
FillAndInit(n, value);
}
Vector(SizeType n)
{
FillAndInit(n, T());
}
Vector(const Vector<T>& v)
{
_start = new T[v.Capacity()];
_end = _start + v.Size();
_endOfStorage = _start + v.Capacity();
for (size_t idx = 0; idx < v.Size(); ++idx)
_start[idx] = v[idx];
}
Vector<T>& operator=(const Vector<T>& v)
{
if(this!=&v)
{
Iterator tmp=_start;
_start = new T[v.Capacity()];
_end = _start + v.Size();
_endOfStorage = _start + v.Capacity();
delete[] tmp;
for (size_t idx = 0; idx < v.Size(); ++idx)
_start[idx] = v[idx];
}
return *this;
}
~Vector()
{
if(_start)
{
delete[] _start;
_start=0;
_end=0;
_endOfStorage=0;
}
}
/Capacity//
SizeType Capacity()const
{
return _endOfStorage-_start;
}
SizeType Size()const
{
return _end-_start;
}
SizeType MaxSize()const
{
return (SizeType)(-1)/sizeof(ValueType);
}
bool Empty()const
{
return _start==_end;
}
//Acess/
Reference operator[](size_t index)
{
return _start[index];
}
ConstReference operator[](size_t index)const
{
return _start[index];
}
Reference Front()
{
return *Begin();
}
ConstReference Front()const
{
return *Begin();
}
Reference Back()
{
return *(End()-1);
}
ConstReference Back()const
{
return *(End()-1);
}
/Iterator//
Iterator Begin()
{
return _start;
}
ConstIterator Begin()const
{
return _start;
}
Iterator End()
{
return _end;
}
ConstIterator End()const
{
return _end;
}
//Modify///
void PushBack(const T& value)
{
CheckCapacity();
*_end=value;
_end++;
}
void PopBack()
{
if(Empty())
return ;
_end--;
}
// 在position位置插入元素data
Iterator Insert(Iterator position, const T& data)
{
SizeType pos=position-_start;
CheckCapacity();
position=_start+pos;
SizeType index=_end-position;
for(;index>0;index--)
{
position[index]=position[index-1];
}
*position=data;
_end++;
return position;
}
// 删除position位置的元素
Iterator Erase(Iterator position)
{
SizeType index=0;
for(;index<_end-position-1;index++)
{
position[index]=position[index+1];
}
_end--;
return position;
}
protected:
void FillAndInit(SizeType n, const T& value)
{
SizeType index=0;
_start=new ValueType[n];
for(;index<n;index++)
{
_start[index]=value;
}
_end=_start+n;
_endOfStorage=_end;
}
void CheckCapacity()
{
if(_end==_endOfStorage)
{
SizeType size=_endOfStorage-_start;
SizeType index=0;
Iterator tmp=_start;
_start=new ValueType[size+RESIZE];
for(;index<size;index++)
{
_start[index]=tmp[index];
}
delete[] tmp;
_end=_start+size;
_endOfStorage=_end+RESIZE;
}
}
protected:
Iterator _start;
Iterator _end;
Iterator _endOfStorage;
};
以模板模拟实现Vector容器
最新推荐文章于 2023-02-13 16:44:51 发布