C/C++實現
template<typename Object>
class Vector
{
private:
int Size;
int Capacity;
Object * objects;
public:
explicit Vector(int initSize = 0);
Vector(const Vector& rhs);
Vector(Vector&& rhs);
Vector&operator=(const Vector&rhs)
{
Vector copy = rhs;
swap(*this, copy);
return *this;
}
Vector&operator=(Vector&&rhs)
{
swap(Size, rhs.Size);
swap(Capacity, rhs.Capacity);
swap(objects, rhs.objects);
return *this;
}
Object& operator[](int index)
{
if (index >= 0 && index < Size)
return objects[index];
else
throw "index out of bounds";
}
const Object&operator[](int index)const
{
if (index >= 0 && index < Size)
return objects[index];
else
throw "index out of bounds";
}
~Vector() { delete[]objects; }
public:
static const int SPARE_CAPACITY = 16;
class const_iterator
{
protected:
Object * current;
const Vector<Object>*theVect;
const_iterator(Object *p,const Vector<Object>&vect):current(p),theVect(&vect){}
Object& retrieve()const
{
assertIsValid();
return *current;
}
void assertIsValid()const
{
if (theVect == nullptr || current == nullptr)
throw "IteratorOutOfBounds";
}
friend class Vector<Object>;
public:
const_iterator() :current(nullptr) {}
const Object &operator*()const {}
const_iterator & operator++()
{
current++;
return *this;
}
const_iterator operator++(int)
{
const_iterator old = *this;
++(*this);
return old;
}
bool operator==(const const_iterator & rhs)const { return current == rhs.current; }
bool operator!=(const const_iterator & rhs)const { return !(*this == rhs); }
};
class iterator :public const_iterator
{
protected:
iterator(Object*p,const Vector<Object>&vect):const_iterator(p, vect) {}
friend class Vector<Object>;
public:
Object&operator*() { return const_iterator::retrieve(); }
const Object & operator*()const { return const_iterator::operator*(); }
iterator & operator++()
{
const_iterator::current++;
return *this;
}
iterator & operator++(int)
{
iterator old = *this;
++(*this);
return old;
}
bool operator==(const iterator & rhs)const { return const_iterator::current == rhs.current; }
bool operator!=(const iterator& rhs)const { return !(*this == rhs); }
};
iterator begin() { return iterator(&objects[0], *this); }
const_iterator cbegin() const { return const_iterator(&objects[0], *this); }
iterator end() { return iterator(&objects[Size], *this); }//超尾迭代器
const_iterator cend() { return const_iterator(&objects[Size], *this); }//超尾迭代器
iterator insert(iterator pos, const Object & x)
{
iterator iter(&objects[0], *this);
Object * oldArray = objects;
++Size;
int i = 0;
if (Capacity < Size)
Capacity = Size;
objects = new Object[Capacity];
while (iter != pos)
{
objects[i] = move(oldArray[i]);
++iter;
++i;
}
objects[i] = x;
int original = i;
++i;
for (; i < Size; ++i)
{
objects[i] = move(oldArray[i-1]);
}
delete[]oldArray;
return iterator(&objects[original], *this);
}
void resize(int newsize)
{
if (newsize > Capacity)
reserve(newsize * 2);
Size = newsize;
}
void reserve(int newCapacity);
bool empty()const { return Size == 0; }
int size()const { return Size; }
int capacity()const { return Capacity; }
void push_back(const Object & x)
{
if (Size == Capacity)
{
reserve(2 * Capacity + 1);
}
objects[Size++] = x;
}
void push_back(const Object && x)
{
if (Size == Capacity)
{
reserve(2 * Capacity + 1);
}
objects[Size++] = std::move(x);
}
void pop_back() { --Size; }
const Object& back()const { return objects[Size - 1]; }
};
template<typename Object>
Vector<Object>::Vector(int initSize):Size(initSize),Capacity(initSize + SPARE_CAPACITY)
{
objects = new Object[Capacity];
}
template<typename Object>
Vector<Object>::Vector(const Vector & rhs) :Size(rhs.Size), Capacity{rhs.Capacity},objects(new Object[Capacity])
{
for (int i = 0; i < Size; ++i)
{
objects[i] = rhs.objects[i];
}
}
template<typename Object>
Vector<Object>::Vector(Vector && rhs):Size(rhs.Size), Capacity{ rhs.Capacity }, objects(rhs.objects)
{
rhs.objects = nullptr;
rhs.Size = 0;
rhs.Capacity = 0;
}