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

来源:互联网 发布:php上传文件 身份 编辑:程序博客网 时间:2024/05/20 09:21
//===============================================//一个简单的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 _Tyvalue;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_ucout = 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];}///////////从后面插入元素////////////////////////////voidpush_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;}}/////////从后面弹出元素///////////////////////////valuepop_back(){if(empty())assert("Tvector为空!");value tem = *_pLast;memset(_pLast,0,sizeof(value));--_pLast;--_len;return tem;}//////////删除pos位置元素///////////////////////itorearse(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;}itorearse(itor _begin,itor _end){}//////////在pos后面插入元素///////////////////itorinsert(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;}}itorinsert(itor pos,value val,size_u n){}///////////清空元素//////////////////////voidclear(){memset(_pFirst,0,sizeof(value)*(_len + 1));_pLast = _pFirst + 1;_len = 0;}///////////是否为空//////////////////////boolempty(){return 0 == _len;}///////////返回元素个数//////////////////////size_usize(){return _len;}///////////返回开始元素指针//////////////////////itorbegin(){return _pFirst;}///////////返回最后元素指针//////////////////////itorend(){return _pLast;}private: voidmove_forward(itor des,itor res,size_u n){if(des == res || n < 1)return;for(size_u i = 0;i < n;++i)*des++ = *res++;}voidmove_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;}itorallot_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;}voiddestory(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;}

测试结果



0 0
原创粉丝点击