一个简单的vector模版,实现了vector的简单功能

//===============================================
//一个简单的vector模版,实现了vector的简
//单功能。申请内存,通过指向前后的2
//个指针,不断的更新数据。
//插入,和删除比较麻烦,需要对数据进行移动。
//===============================================


//===============================================
//这个文件是Tvector.h文件
//===============================================
#pragma once
////////每次分配内存的大小的增量
#define SIZE_ONCE 100
#include <iostream>
#include <assert.h>

typedef unsigned int size_u;

template<class _Ty>
class Tvector
{
public:
	typedef _Ty			value;
	typedef value*		itor;
public:
//////////////缺省构造函数/////////////////////////////////////////
	Tvector()
	{
		_size = SIZE_ONCE;
		_len = 0;
		_pFirst = new value[_size + 1];
		memset(_pFirst,0,sizeof(value)*(_size + 1));
		_pLast = _pFirst;
	}
	//////////////带参构造函数/////////////////////////////////////////
	Tvector(value val,size_u n)
	{
		if(n < 1)
		{
			Tvector();
			return ;
		}
		_size = SIZE_ONCE;
		size_u	cout = n / _size + 1;
		_size = cout * SIZE_ONCE;
		_pFirst = new value[_size + 1];
		_pLast = _pFirst;
		for(size_u i = 0; i < n; ++i)
			*_pLast++ = val;
		_len = n;
	}
	///////////////析构函数/////////////////////////////////////////
	~Tvector()
	{
		destory(_pFirst);
	}

	/////////////下标访问//////////////////////////////////////
	value& operator[] (size_u index)
	{
		if(empty())
			assert("Tvector为空!");
		if (index < 0 || index > _len - 1)
			assert("pos越界!");
		return _pFirst[index];
	}

	////////////获取某个元素////////////////////////////////
	value&	at(size_u pos)
	{
		if(empty())
			assert("Tvector为空!");
		if (pos < 0 || pos > _len - 1)
			assert("pos越界!");
		return _pFirst[pos];
	}

	///////////从后面插入元素////////////////////////////
	void	push_back(value val)
	{
		itor tem = allot_again(1);
		if(tem)
		{
			itor _pOldF = _pFirst;
			_pFirst = (itor)memcpy(tem,_pFirst,sizeof(value)*_len);
			_pLast = _pFirst + _len++;
			*_pLast++ = val;
			destory(_pOldF);
		}
		else
		{
			*(_pLast++) = val;
			++_len;
		}
	}
	/////////从后面弹出元素///////////////////////////
	value		pop_back()
	{
		if(empty())
			assert("Tvector为空!");

		value tem = *_pLast;
		memset(_pLast,0,sizeof(value));
		--_pLast;
		--_len;
		return tem;
	}

	//////////删除pos位置元素///////////////////////
	itor	earse(itor _pos)
	{
		if(_pos == _pLast)
			assert(false);
		size_u _si = _pLast - _pos;
		move_forward(_pos,_pos + 1,_si - 1);
		memset(_pLast,0,sizeof(value));
		--_pLast;
		--_len;
		return _pos;
	}

	itor	earse(itor _begin,itor _end)
	{

	}

	//////////在pos后面插入元素///////////////////
	itor	insert(itor pos,value val)
	{
		if(pos == _pLast || empty())
			assert(false);

		itor tem = allot_again(1);
		if(tem)
		{
			itor _pOldF = _pFirst;
			int a = int(pos - _pFirst) + 1;
			int b = int(_pLast - pos - 1);
			_pFirst =(itor) memcpy(tem,_pFirst,a*sizeof(value));
			*(_pFirst + a ) = val;
			memcpy(_pFirst + a + 1,pos + 1,b*sizeof(value));
			_len++;
			_pLast = _pFirst + _len + 1;
			destory(_pOldF);
			return (_pFirst + a + 1);
		}
		else
		{
			move_backward(pos + 1,pos + 2,_pLast - pos -1 );
			*(pos+1) = val;
			++_pLast;
			++_len;
			return pos;
		}
	}
	itor	insert(itor pos,value val,size_u n)
	{
	}
	///////////清空元素//////////////////////
	void	clear()
	{
		memset(_pFirst,0,sizeof(value)*(_len + 1));
		_pLast = _pFirst + 1;
		_len = 0;
	}

	///////////是否为空//////////////////////
	bool	empty()
	{
		return 0 == _len;
	}

	///////////返回元素个数//////////////////////
	size_u	size()
	{
		return _len;
	}
	///////////返回开始元素指针//////////////////////
	itor	begin()
	{
		return _pFirst;
	}

	///////////返回最后元素指针//////////////////////
	itor	end()
	{
		return _pLast;
	}

private: 

	void	move_forward(itor des,itor res,size_u n)
	{
		if(des == res || n < 1)
			return;
		for(size_u i = 0;i < n;++i)
			*des++ = *res++;
	}
	void	move_backward(itor res,itor des,size_u n)
	{
		if(des == res || n < 1)
			return;
		itor tem = new value[n];
		memcpy(tem,res,n*sizeof(value));
		memcpy(des,tem,n*sizeof(value));
		delete[] tem;
	}

	itor	allot_again(size_u _n)
	{
		itor tem = NULL;
		if(_len + _n > _size)
		{
			_size = ((_len + _n) / SIZE_ONCE + 1)*SIZE_ONCE ;
			tem = new value[_size + 1];
			memset(tem,0,sizeof(value)*(_size+1));
		}
		return tem;
	}

	void	destory(itor tem)
	{
		if(tem)
			delete[] tem;
	}

private:
	itor	_pFirst;
	itor	_pLast;
	size_u	_size;
	size_u	_len;
};

测试

//
#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <string>
#include "Tvecor.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string kstr1 = "cainiao1";
	string kstr2 = "cainiao2";
	string kstr3 = "cainiao3";
	string kstr4 = "cainiao4";
	string kstr5 = "cainiao5";
	string kstr6 = "cainiao6";
	string kstr7 = "cainiao7";

	Tvector<string> kvet;
	kvet.push_back(kstr1);
	kvet.push_back(kstr2);
	kvet.push_back(kstr3);
	kvet.push_back(kstr4);
	
	for(Tvector<string>::itor it = kvet.begin();it!=kvet.end();++it)
	{
		cout<< *it << endl;
	}
	cout<<endl;

	kvet.push_back(kstr5);
	kvet.push_back(kstr6);
	for (unsigned int i=0; i<kvet.size();++i)
	{
		cout<<kvet[i] << endl;
	}
	cout<<endl;

	kvet.earse(kvet.begin()+2);
	for (unsigned int i=0; i<kvet.size();++i)
	{
		cout<<kvet[i] << endl;
	}
	cout<<endl;	

	kvet.pop_back();
	for (unsigned int i=0; i<kvet.size();++i)
	{
		cout<<kvet[i] << endl;
	}
	cout<<endl;	

	system("pause");
	return 0;
}

测试结果



原文地址:https://www.cnblogs.com/byfei/p/6389689.html