以模板模拟实现Vector容器

#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;

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值