自定义vector
代码:
//自定义vector实现STL中vector的主要功能,包含自定义,还要加上insert,erase,inverse,find等函数成员
//提示:需要复制粘贴到visual studio 进行测试
#define VECTOR_H
#include <algorithm>
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename Object>
class Vector
{
//构造函数和无参构造函数
public:
explicit Vector(int initSize = 0)
: theSize{ initSize }, theCapacity{ initSize + SPARE_CAPACITY }
{
objects = new Object[theCapacity];
}
//复制构造函数和复制赋值函数
Vector(const Vector & rhs)
: theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ nullptr }
{
objects = new Object[theCapacity];
for (int k = 0; k < theSize; ++k)
objects[k] = rhs.objects[k];
}
Vector & operator= (const Vector & rhs)
{
Vector copy = rhs;
std::swap(*this, copy);
return *this;
}
~Vector()
{
delete[] objects;
}
//移动构造函数和移动赋值