vector一些操作的练习

#include <iostream>
#include <assert.h>
using namespace std;

template<typename T>
class Vector
{
public:
	typedef T ValueType;
	typedef ValueType* Iterator;
	typedef const ValueType* ConstIterator;
	typedef ValueType& Reference;
	typedef const ValueType& ConstReference;
	typedef size_t SizeType;

	Vector();
	Vector(size_t n, const T&value = Vector());
	Vector(const Vector<T>& v);
	Vector& operator=(const Vector<T>& v);
    Iterator Begin();
	Iterator End();
	const Iterator Begin()const;
	const Iterator End()const;
	SizeType Size()const;
	SizeType Capacity()const;
	bool Empty()const;
	Reference operator[](size_t index);
	ConstReference operator[](size_t index)const;
	void PushBack(const T& value);
	void PopBack();
	Iterator Insert(Iterator pos, const T& value);
	Iterator Erase(Iterator pos);
	void Clear()const;
	void ReSize(SizeType newSize, const T& value = T());
	~Vector();
protected:
	Iterator _start;
	Iterator _finish;
	Iterator _endofStorage;
private:
	void CheckCapacity()
	{
		if ((_endofStorage - _finish)>0)
			return ;
		size_t newCapacity = Capacity()*2+1;
		size_t size = _finish - _start;
		Iterator temp = new T[newCapacity];
		if (NULL == temp)
			exit(0);
		memcpy(temp,_start,sizeof(T)*size);
		delete []_start;
		_start = temp;
		_finish = _start+size;
		_endofStorage = _start+newCapacity;
	}
};

template <class T>
Vector<T>::Vector()
:_start(0),
 _finsh(0),
 _endofStorage(0)
{}

template <class T>
Vector<T>::Vector(size_t n, const T& value)
:_start(new T[n])
{
	for (size_t i = 0; i<n; i++)
	{
		_start[i] = value; 
	}
	_finish = _start + n;
	_endofStorage = _finish;
}

template <class T>
Vector<T>::Vector(const Vector<T>& v)
{
	size_t len = v._finish - v._start;
	_start = new T[len];
	for (size_t i = 0; i<len; ++i)
	{
		_start[i] = v._start[i];
	}
	_finish = _start+len;
	_endofStorage = _finish;
}

template <class T>
Vector<T>& Vector<T>::operator=(const Vector<T>& v)
{
	if(this != &v)
	{
		size_t len = v._finish - v._start;
		if (this->Capacity() >= len)
		{
			for (int i = 0; i<len; ++i)
			{
				_start[i] = v._start[i];
			}
		}
		else 
		{
			Iterator temp = new T[len];
		    delete []_start;
			_start = temp;
		}
	}
	return *this;
}

template <class T>
T* Vector<T>::Begin()
{
	return (this->_start);
}

template <class T>
T* Vector<T>::End()
{
	return (this->_finish);
}

template <class T>
T* const Vector<T>::Begin()const
{
	return (this->_start);
}

template <class T>
 T* const Vector<T>::End()const
{
	return (this->_finish);
}

template <class T>
size_t Vector<T>::Size()const
{
	return (_finish - _start);
}

template <class T>
size_t Vector<T>::Capacity()const
{
	return (_endofStorage - _start);
}

template <class T>
bool Vector<T>:: Empty()const
{
	return 0==(_finish - _start);
}

template <class T>
T& Vector<T>:: operator[](size_t index)
{
	assert(index< Size());
	return _start[index];
}

template <class T>
const T& Vector<T>::operator[](size_t index)const
{
	assert(index < Size());
	return _start[index];
}

template <class T>
void Vector<T>:: PushBack(const T& value)
{
	CheckCapacity();
	*_finish = value;
	_finish += 1;
}

template <class T>
void Vector<T>::PopBack()
{
	if (_finish - _start)
		_finish -= 1;
}

template <class T>
T* Vector<T>::Insert(Iterator pos, const T& value)
{
	CheckCapacity();
	assert(pos);
	if (pos == _finish)
		PushBack(value);
	size_t size = Size() - (pos - _start);

	for (size_t sz = size; sz>0 ; sz-- )
	{
		*(pos+sz) = *(pos+sz-1);
	}
	*pos = value;
	return pos;

}

template <class T>
T* Vector<T>::Erase(Iterator pos)
{
	assert(pos);
	if (pos == _finish)
		PopBack();
	size_t size = Size() - (pos - _start);

	for (size_t sz = 0; sz<size ; sz++ )
	{
		*(pos+sz) = *(pos+sz+1);
	}
	_finish -= 1;
	return pos;
	
}

template <class T>
void Vector<T>::Clear()const
{
	delete []_start;
	_start = NULL;
	_finish = NULL;
	_endofStorage = NULL;
}

template <class T>
void Vector<T>:: ReSize(SizeType newSize, const T& value = T())
{
    if (newSize < Size())  
    {  
        _endofStorage = _start + newSize;  
        _finish = _endofStorage;  
    }  
    else  
    {  
        Iterator Temp = new T[newSize];  
        delete []_start;  
        _start = Temp;  
        _finish = _start + newSize;  
        _endofStorage = _finish;  
    }  
    for (size_t i = 0; i <Capacity(); i++)  
    {  
        _start[i] = value;  
    }  
}

template <class T>
Vector<T>::~Vector()
{
	delete []_start;
}


template <class T>
void show(Vector<T> v)
{
	Vector<T>::Iterator it;
	for (it = v.Begin(); it < v.End(); ++it)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
}

void  Funtest1()
{
	Vector <int>v1(3,5);
	Vector <int>v2(v1);
	v2.PushBack(4);
	v1.PopBack();
	Vector<int>::Iterator it = v2.Begin() +4;
	v2.Insert(it,6);
	show(v2);
	it = v2.Begin() +3;
	v2.Erase(it);
	show(v2);
}

int main()
{
	Funtest1();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魏尔肖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值