【C++】动态向量容器类DynamicVector

前段时间比较忙,所以好久没更新了。。。。

两个月前在学深浅copy的时候接触了动态数组。在之后的练习题当中,动态数组逐渐无法满足难度需求,遂将动态数组升级成了动态向量容器(类似于C++的Vector类)。本来以为只是简单的照葫芦画瓢,但是经过不断地在类中添加功能,动态向量容器类越来越复杂,想来还是有必要完整地梳理总结一下。

题目要求:

> 实现一个DynamicVector类,要求实现动态向量容器的相关功能。
> 
> 类中需要包含的成员函数有:
> 
> 1、析构函数~DynamicVector();   用来释放本容器所占用的空间。
> 
> 2、增加一个void push_back (const T& newItem); 要求:如果numofItems <
> mallocSize时,那么向量空间大小不变,在已存元素的最后,添加一个newItem元素,并且numofItems++;如果numofItems
> == mallocSize时,那么向量空间大小增加到(2 * mallocSize +1),原有元素内容不变,在原有元素后面添加一个newItem元素,并且numofItems++; 注意归还相应内存。
> 
> 3、增加一个拷贝构造函数。
> 
> 4、增加一个=重载,实现深赋值。
> 
> 5、重载push_back函数,void push_back (const DynamicVector&);
> 
> 6、增加一个逻辑==重载。只要两个向量容器的已经存放的元素对应相等,且起始的下标相等,就是相等的(即使分配的空间大小不同也没关系),返回true。否则返回false> 
> 7、增加一个输出操作符操作符<<重载。如果是一个空向量容器,输出 cout << "The arrray is empty." <<
> endl; 否则 空格分隔输出每个元素。
> 
> 8、增加一个void insert (int Vindex, const T&); 
> 在下标为Vindex处插入一个元素,后面元素向后移动,如果空间不够则加空间。
> 
> 9、实现三个remove函数:  void remove ();   //删除最后一个元素。  void remove (int
> Vindex); //删除Vindex号元素。  void remove (int Vfirst, int Vlast);  
> //删除[Vindex,Vlast) 号元素。 注意是左闭右开集合,即不删除Vlast元素
> 10、增加 DynamicVector<T> operator() (int Vfirst, int Vlast) const
> 该函数对象功能为返回 [Vindex,Vlast) 号元素构成的DynamicVector<T> 对象。
> 
> 11、 增加一个交换函数,实现两个对象的内容交换。 
> void swap(DynamicVector<T> & dv); ```

DynamicVector类:

//DynamicVector类:


template <class T>
class DynamicVector
{
	private:
		T* array;  //指向分配空间的指针 
		unsigned mallocSize;  //分配空间的大小 
		unsigned numofItems;  //向量内已经存储的元素数量
		int virtualZero;  //数组起始下标,C++中通常数组下标是从0开始,这个数据属性可以让数组的下标从任意整数开始 ,让数组更加灵活
	public:
		DynamicVector(int Vindex)  //构造函数 
		{
			array=NULL;
            numofItems=0;
            mallocSize=0;
            virtualZero=Vindex;
		}
		DynamicVector(const DynamicVector<T> &anotherDA)  //拷贝构造函数 
		{
			int i;
	        array=new T[anotherDA.mallocSize]; 
	        mallocSize=anotherDA.mallocSize;
	        virtualZero=anotherDA.virtualZero;
	        numofItems=anotherDA.numofItems; 
	        for(i=0;i<mallocSize;i++)
	        {
		        array[i]=anotherDA.array[i];
	        }
		}
		~DynamicVector()   //析构函数 
		{
			delete [] array;
		}
		inline void push_back(const T& newItem)  //push_back函数 
		{
			if(numofItems<mallocSize)
			{
				array[numofItems]=newItem;
				numofItems++;
			}
			else if (numofItems==mallocSize)
			{
				int i;
				T* arrays=array;
				array=new T[2*mallocSize+1];
				mallocSize=2*mallocSize+1; 
				for (i=0;i<numofItems;i++)
				{
					array[i]=arrays[i];
				}
				array[numofItems] = newItem;
				numofItems++;
				delete [] arrays;
			}
		}
		void push_back(const DynamicVector &t)  //push_back函数的重载 
		{
			if(numofItems+t.numofItems<=mallocSize) 
			{
				int i;
				for(i=numofItems;i<numofItems+t.numofItems;i++)
				{
					array[i]=t.array[i-numofItems];
				}
				numofItems=numofItems+t.numofItems;
			}
			else if(numofItems+t.numofItems>mallocSize)
			{
				int i;
				T* arrays;
				arrays=array;
				array=new T[2*mallocSize+1];
				mallocSize=2*mallocSize+1; 
				for (i=0;i<numofItems;i++)
				{
					array[i]=arrays[i];
				}
			    for(i=numofItems;i<numofItems+t.numofItems;i++)
			    {
			      array[i]=t.array[i-numofItems];	
			    }
				numofItems=numofItems+t.numofItems;
				delete [] arrays;
			}
		}
		DynamicVector<T> &operator = ( const DynamicVector<T> &anotherDA)  //深层赋值=重载 
		{
			if ( this == & anotherDA)
			{
				return *this;
			}
			delete [] array;
			array=new T[anotherDA.mallocSize];
			mallocSize=anotherDA.mallocSize; 
			virtualZero=anotherDA.virtualZero;
			numofItems=anotherDA.numofItems;
			int i; 
			for(i=0;i<anotherDA.mallocSize;i++)
			{
				array[i]=anotherDA.array[i];
			} 
			return *this;
		} 
		T& operator [] (int Vindex)  //[]重载 
		{
            int _entry=Vindex-virtualZero;
	        if(_entry<0||_entry>=numofItems)
	        {
		        cout<<endl<<"Out Of Range";
		        exit(1);
	        }
	        return array[_entry];
		}
		bool operator == (const DynamicVector<T>& dv) const  //bool型==重载 
		{
			int i;
			int flag=1;
			for(i=0;i<numofItems;i++)
			{
				if(array[i]!=dv.array[i])
				{
					flag=0;
				}
			}
			if(flag==1&&numofItems==dv.numofItems)
			return true;
			else return false;
		}
		unsigned length() const  //返回向量内已经存储的元素数量
		{
			return numofItems;
		}
		unsigned capacity() const  //返回分配空间的大小
		{
			return this->mallocSize;
		}
		int firstIndex() const  //返回数组起始下标
		{
			return this->virtualZero;
		}
		friend ostream& operator << (ostream& out,DynamicVector<T> &t)  //输出重载 
		{
			if(t.numofItems==0)
			{
				cout<<"The arrray is empty."<<endl;
			}
			else
			{
				int i;
				for(i=0;i<t.numofItems;i++)
				{
					cout<<t.array[i]<<" ";
				}
			}
			return out;
		} 
		void insert (int Vindex, const T &z)  //插入一个元素 z
		{
            Vindex=Vindex-virtualZero;
	        mallocSize=mallocSize+1;
	        int i;
	        for(i=numofItems;i!=Vindex;i--)
	        {
		        array[i]=array[i-1];
	        }
	        array[Vindex]=z;
	        numofItems=numofItems+1;
		} 
		void remove()  //删除最后一个元素——(1) 
		{
			T* arrays;
			arrays=array;
			array=new T[mallocSize];
			int i;
			for(i=0;i<numofItems-1;i++)
			{
				array[i]=arrays[i];
			}
			numofItems--;
		}
        void remove(int Vindex) //删除下标为Vindex的元素,要记得减初始下标 ——(2) 
        {
        	int i;
        	T* arrays=array;
        	array=new T[mallocSize];
			for(i=0;i<Vindex-virtualZero;i++)
			{
				array[i]=arrays[i];
			} 
			for(i=Vindex-virtualZero;i<numofItems-1;i++)
			{
				array[i]=arrays[i+1];
			}
			numofItems=numofItems-1;
			delete [] arrays;
        }
        void remove(int Vfirst, int Vlast)   //删除[Vindex,Vlast)号元素。注意是左闭右开集合,即不删除Vlast元素——(3) 
        {
        	int i;
			int t=0;
        	T* array1=array;
        	array=new T[mallocSize];
        	for(i=0;i<Vfirst-virtualZero;i++)
        	{
        		array[t]=array1[i];
        		t++;
        	}
        	for(i=Vlast-virtualZero;i<numofItems;i++)
        	{
        		array[t]=array1[i];
        		t++;
        	}
        	numofItems=numofItems-Vlast+Vfirst;
        	mallocSize=mallocSize-sizeof(T)*(Vlast-Vfirst);
        	delete [] array1;
        }
        DynamicVector<T> operator() (int Vfirst,int Vlast) const  //()重载 
        {
        	int v1,v2,i;
        	v1=Vfirst+2;
        	v2=Vlast+2;
        	DynamicVector t(-2);
        	t.numofItems=v2-v1;
        	t.mallocSize=2*t.numofItems+1;
        	t.array=new T[mallocSize];
        	for(i=0;i<t.numofItems;i++)
        	{
        		t.array[i]=array[v1];
        		v1++;
			}
			return t;
		}
		void swap(DynamicVector<T> &dv)  //交换两个对象的内容的函数 
		{
			int i,n,m;
			T* array1=new T[dv.numofItems];
			for(i=0;i<dv.numofItems;i++)
			{
				array1[i]=dv.array[i];
			}
			n=dv.numofItems;
			m=dv.mallocSize;
			delete [] dv.array;
			dv.array=new T[mallocSize];
			for(i=0;i<numofItems;i++)
			{
				dv.array[i]=array[i];
			}
			dv.numofItems=numofItems;
			dv.mallocSize=mallocSize;
			delete [] array;
			array=new T[m];
			numofItems=n;
			array=array1;
		}
		T* begin() const
    	{
		    return &array[0];
        }
        T* end() const
        {
        	return &array[numofItems];
	    }
        DynamicVector(T* const first,T* const last,int Vindex=0)
        {
    	    int i;
            numofItems=last-first;
            virtualZero=Vindex;
            mallocSize=2*numofItems+1;
    	    array=new T[mallocSize];
    	    array[0]=1.1;
    	    for(i=1;i<numofItems;i++)
    	    array[i]=array[i-1]+1;
	    }
};

测试用主函数:

int main()
{
    DynamicVector<int> ra(-2);
    int i,n;
    cin>>n;
    cout<<ra;
    ra.push_back(-3);
    ra.push_back(-2);
    ra.push_back(-1);
    for(i=0;i<n;i++)
    {
      ra.push_back(i);
    }
    cout<<"\n malloSize is "<<ra.capacity();
    cout<<"\n numofItems is "<<ra.length();
    cout<<"\n StartIndex is "<<ra.firstIndex()<<endl;
    for(i=-2;i<n+1;i++)
    {
      cout<<ra[i]<<" ";      
    }
    cout<<endl;
    DynamicVector<int> raCopy(ra);
    cout<<"\n malloSize is "<<raCopy.capacity();
    cout<<"\n numofItems is "<<raCopy.length();
    cout<<"\n StartIndex is "<<raCopy.firstIndex()<<endl;
    cout<<endl;
    for(i=-2;i<n+1;i++)
    {   
	    cout<<++ra[i]<<" ";      
    }
    cout<<endl;
    for(i=-2;i<n+1;i++)
    {   
	    cout<<raCopy[i]<<" ";      
    }
    raCopy=ra;
    if (ra==raCopy)  cout<<"\n ra == raCopy";
    else cout<<"\n ra != raCopy";
    ra[-2]=100;
    if (ra==raCopy)  cout<<"\n ra == raCopy";
    else cout<<"\n ra != raCopy";
    raCopy.push_back(ra);
    cout<<endl;
    int firstI=raCopy.firstIndex();
    for(i=0;i<raCopy.length();i++)
    {   
	    cout<<raCopy[i+firstI ]<<" ";      
    }
    cout<<endl;
    raCopy.insert(-2,6);
    raCopy.insert(-1,7);
    cout<<raCopy;
    raCopy.remove();    
    cout<<endl;
    cout<<raCopy<<" remove()";
    raCopy.remove(-1);    
    cout<<endl;
    cout<<raCopy<<" remove(-1)";
    raCopy.remove(-1,1);    
    cout<<endl;
    cout<<raCopy<<" remove(-1,1)";
    ra=raCopy(-1,3);    
    cout<<endl;
    cout<<ra<<" raCopy(-1,3)";
    ra.swap(raCopy);    
    cout<<endl<<"ra.swap(raCopy)"<<endl;
    cout<<ra;
    cout<<endl;
    cout<<raCopy;
    return 0;
}

测试输入1:

5

测试输出1:

The arrray is empty.

malloSize is 15
numofItems is 8
StartIndex is -2
-3 -2 -1 0 1 2 3 4

malloSize is 15
numofItems is 8
StartIndex is -2

-2 -1 0 1 2 3 4 5
-3 -2 -1 0 1 2 3 4
ra == raCopy
ra != raCopy
-2 -1 0 1 2 3 4 5 100 -1 0 1 2 3 4 5
6 7 -2 -1 0 1 2 3 4 5 100 -1 0 1 2 3 4 5
6 7 -2 -1 0 1 2 3 4 5 100 -1 0 1 2 3 4 remove()
6 -2 -1 0 1 2 3 4 5 100 -1 0 1 2 3 4 remove(-1)
6 0 1 2 3 4 5 100 -1 0 1 2 3 4 remove(-1,1)
0 1 2 3 raCopy(-1,3)
ra.swap(raCopy)
6 0 1 2 3 4 5 100 -1 0 1 2 3 4
0 1 2 3

测试输入2:

6

测试输出2:

The arrray is empty.

 malloSize is 15
 numofItems is 9
 StartIndex is -2
-3 -2 -1 0 1 2 3 4 5

 malloSize is 15
 numofItems is 9
 StartIndex is -2

-2 -1 0 1 2 3 4 5 6
-3 -2 -1 0 1 2 3 4 5
 ra == raCopy
 ra != raCopy
-2 -1 0 1 2 3 4 5 6 100 -1 0 1 2 3 4 5 6
6 7 -2 -1 0 1 2 3 4 5 6 100 -1 0 1 2 3 4 5 6
6 7 -2 -1 0 1 2 3 4 5 6 100 -1 0 1 2 3 4 5  remove()
6 -2 -1 0 1 2 3 4 5 6 100 -1 0 1 2 3 4 5  remove(-1)
6 0 1 2 3 4 5 6 100 -1 0 1 2 3 4 5  remove(-1,1)
0 1 2 3  raCopy(-1,3)
ra.swap(raCopy)
6 0 1 2 3 4 5 6 100 -1 0 1 2 3 4 5
0 1 2 3
  • 12
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jeron Zhou

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

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

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

打赏作者

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

抵扣说明:

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

余额充值