Vector.h
#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;
class Vector
{
public:
Vector()//构造函数
: first(NULL)
, finish(NULL)
, end(NULL)
{}
size_t size() const//当前已用容量
{
return finish - first;
}
size_t capacity() const//最大容量
{
return end - first;
}
void reverse(size_t n)//预留空间
{
if (n > capacity())
{
expand(n);
}
}
void resize(size_t n, DataType value)//设置分配空间,并对多开空间赋值
{
if (n < size())//缩小
{
finish = first + n;
}
else
{
if (n > capacity())
{
expand(n);//扩容
}
size_t index = n - size();
while (--index)
{
*finish = value;
finish++;
}
}
}
Vector(const Vector& v)//拷贝构造 v1(v2)
//为避免浅拷贝,需用深拷贝另开空间
{
DataType *tmp = new DataType[v.size()];
first = tmp;
finish = first + v.size();
end = first + v.size();
memcpy(first, v.first, sizeof(DataType)*v.size());