Vector.h

#pragma once

#include <iostream>
#include <assert.h>

template<class T>
class Vector
{
public:
	typedef T* Iterator;
	typedef const T* ConstIterator;
	typedef T* Pointer;
	typedef const T* ConstPointer;
	typedef T& Reference;
	typedef const T& ConstReference;
	typedef ptrdiff_t DifferenceType;
	typedef Vector<T> Self;
public:
	Vector()
		:_start(NULL)
		, _finsh(NULL)
		, _endOfStorage(NULL)
	{}
	Iterator Begin()
	{
		return _start;
	}
	ConstIterator Begin()const
	{
		return _start;
	}
	Iterator End()
	{
		return _finsh;
	}
	ConstIterator End()const
	{
		return _finsh;
	}
	Reference Front()
	{
		return *Begin();
	}
	ConstReference Front()const
	{
		return *Begin();
	}
	Reference Back()
	{
		return *(End() - 1);
	}
	ConstReference Back()const
	{
		return *(End() - 1);
	}
	void PushBack(const T& x)
	{
		Insert(_finsh,x);
	}
	void PopBack()
	{
		--_finsh;//直接丢弃finsh后面的数据
	}
	Iterator Insert(Iterator pos,const T& x)
	{
		size_t n = pos - _start;
		size_t oldsize = Size();
		if (oldsize == 0){//1、此时没有一个数据,可用空间为0,需要扩容
			_start = new T(x);
			_finsh = _start + 1;
			_endOfStorage = _finsh;
		}
		else if (pos == End() && _finsh < _endOfStorage){//2、尾插且不需要扩容
			_start[oldsize] = x;
			++_finsh;
		}
		else{
			//3、非尾插
			_CheckEndOfStorage();
			Iterator tmp = _finsh;
			pos = _start + n;//**************** very importent  有可能扩容,pos就不是要插入的那个位置了
			while (pos <= tmp){
				*(tmp + 1) = *tmp;
				--tmp;
			}
			*pos = x;
			++_finsh;
		}
		return _start+n;
	}
	//iterator insert(iterator position);
	//iterator insert(iterator position, const T& x);
	//void insert(iterator position, InputIterator first, InputIterator last);
	//void insert(iterator position,const_iterator first, const_iterator last);
	//void insert(iterator pos, size_type n, const T& x);
	//void insert(iterator pos, int n, const T& x);
	//void insert(iterator pos, long n, const T& x);
	Iterator Erase(Iterator pos)
	{
		assert(pos != End());

		size_t size = Size();
		if (size == 0){
			return pos;
		}

		size_t n = pos - _start;
		Iterator tmp = pos;
		while (tmp < End()-1){
			*tmp = *(tmp + 1);
			tmp++;
		}
		_finsh--;
		return _start + n;
	}
	bool Empty()
	{
		return Begin()==End();
	}
	size_t Size()
	{
		return _finsh - _start;
	}
	void Swap(Self& x)
	{}
	void Clear()
	{}
	Reference operator[](size_t index)
	{
		assert(index >= 0 && index < Size());
		return *(Begin() + index);
	}
	ConstReference operator[](size_t index)const
	{
		assert(index >= 0 && index < Size());
		return *(Begin() + index);
	}
private:
	void _CheckEndOfStorage()//检查容量,如果需要扩容则扩容并把原来的内容拷贝到新的空间,释放原来的空间
	{
		int oldcapacity = Size();
		if (_start != NULL && _finsh < _endOfStorage){
			return;
		}
		int newcapacity = oldcapacity * 2 + 3;
		T* tmp = new T[newcapacity];
		int index = 0;
		while(index<oldcapacity){
			tmp[index] = _start[index];
			index++;
		}
		delete[] _start;
		_start = tmp;
		_finsh = _start + oldcapacity;
		_endOfStorage = _start + newcapacity;
	}
protected:
	T* _start;
	T* _finsh;
	T* _endOfStorage;
};

Test.cpp

#include "Vector.h"
#include <vector>

void PrintVector(Vector<int> v)
{
	size_t size = v.Size();
	Vector<int>::Iterator it = v.Begin();
	while (it!=v.End()){
		std::cout << *it << std::endl;
		++it;
	}
}
void PrintVector2(std::vector<int> v)
{
	size_t size = v.size();
	std::vector<int>::iterator it = v.begin();
	while (it != v.end()){
		std::cout << *it << std::endl;
		++it;
	}
}
void TestVector()
{
	//std::vector<int> v2;
	////v2.push_back(2);
	//std::vector<int>::iterator item;
	//item=v2.begin();
	//v2.erase(item);
	//PrintVector2(v2);

	Vector<int> v;
	v.PushBack(2);
	Vector<int>::Iterator ite;
	ite = v.Begin();
	v.Erase(ite);
	PrintVector(v);

	v.PopBack();
	v.PushBack(1);
	v.PushBack(2);
	v.PushBack(3);
	v.PushBack(4);
	v.PopBack();
	std::cout<<v[0]<<std::endl;
	PrintVector(v);
	//ite = v.End();
	//v.Erase(ite);
	//PrintVector(v);
	/*ite = v.Begin();
	v.Insert(ite,5);
	PrintVector(v);
	v.Erase(ite);
	ite = v.Begin();
	PrintVector(v);*/
	/*ite = v.End();
	v.Erase(ite);*/
	//PrintVector(v);
}

int main()
{
	TestVector();
	
	system("pause");
	return 0;
}