Vector容器的简单实现

/*设计并实现完整的miniVector类,要求增加插入和删除函数,并测试主要的成员函数。
	miniVector的基本功能应该包括以下几项:数组大小可调,
	允许对任一元素赋值,修改,增加,删除并且不影响其他元素的正常访问。
	扩展功能应包括允许通过下标访问,允许将一个对象复制给另一个对象,和其他功能。*/
//----------------------------------Vector.h---------------------------------------
#include<iostream>

#ifndef VECTOR
#define VECTOR

typedef int ElementType;
class Vector
{
public:
    //-------------------------------基本功能-------------------------------------

    explicit Vector(int size=0,int value=0);//构造函数
	Vector(const Vector & original);//复制构造函数
	~Vector();//析构函数
	const Vector & operator=(const Vector & rightHandSide);//赋值

	int capacity() const;//容量
	int size() const;//元素个数

	bool empty() const;//判空
	Vector & reserve(int numCap);//扩容

	void push_back(const ElementType & value);//添加元素于尾部
	void pop_back();//删除尾元素

	ElementType front();//返回第一个元素的引用
	ElementType back();//返回最后一个元素的引用

	ElementType & operator [] (int pos) const;//下标访问(非越界访问)
	ElementType at(int pos);//下标访问(越界检查)

    void swap(Vector & aVector);//交换

	bool operator==(const Vector & rightHandSide) const;//值以及顺序的比较
	bool operator<(const Vector & rightHandSide) const;//字典序比较

	//------------------------------新增功能----------------------------------------

	void insert(int pos,ElementType item);//插入
	void erase(int pos);//删除
	void display(ostream & out) const;//显示所有元素
	//------------------------------------------------------------------------------
private:
	int mySize;
	int myCapacity;
	ElementType *myArray;
};
ostream & operator <<(ostream & out,const Vector & aVector);//重载输出操作符
#endif

//-----------------Vector.cpp-----------------------
#include<cassert>
#include<new>
using namespace std;

#include"Vector.h"

//---------------------------------基本功能--------------------------------------

Vector::Vector(int size,int value)//构造函数----------------
{
	if(size>=0)
	{
	   myCapacity=size;
	   if(value==0)  //如果value为0,则默认将mySize置为0  
	      mySize=0;
	   else
	      mySize=size;
	   myArray=new(nothrow) ElementType[myCapacity];//动态申请数组
	   assert(myArray!=0);       //检查内存是否足够
	   for(int i=0;i<mySize;i++)   //逐个赋值
	       myArray[i]=value;
	}
	else
	{
		cerr<<"***Illegal size to input***\n";//非法输入报错
		exit(1);
	}	
}

Vector::Vector(const Vector & original)//复制构造函数------------------
:mySize(original.mySize),myCapacity(original.myCapacity)
{
	myArray=new(nothrow) ElementType[myCapacity];//动态申请数组
	if(myArray!=0)                   //检查内存是否足够
		for(int pos=0;pos<mySize;pos++)   //逐个赋值
			myArray[pos]=original.myArray[pos];
	else
	{
		cerr<<"***Inadequate memory to allocate vector ***\n";//非法输入报错
		exit(1);
	}
}

Vector::~Vector()//析构函数----------------
{
	delete [] myArray;//回收内存
}

const Vector & Vector::operator=(const Vector & rightHandSide)//赋值-----------------
{
	if(this!=&rightHandSide)//不是自我赋值
	{
		if(myCapacity!=rightHandSide.myCapacity)//容量不等
		{
			delete [] myArray;//销毁原数组

			myCapacity=rightHandSide.myCapacity;
			myArray=new(nothrow) ElementType[myCapacity]; //动态申请数组
			if(myArray==0)
			{
				cerr<<"***Inadequate memory ***\n";//内存不够报错
				exit(1);
			}
		}
		mySize=rightHandSide.mySize;
		for(int pos=0;pos<mySize;pos++)//逐个赋值
			myArray[pos]=rightHandSide.myArray[pos];
	}
	return *this;//返回对象本身
}

int Vector::capacity() const//容量-------------------------
{
	return myCapacity;
}

int Vector::size() const//元素个数------------------------
{
	return mySize;
}

bool Vector::empty() const//判空-------------------------
{
	return (mySize==0);
}

Vector & Vector::reserve(int numCap)//扩容----------------------
{
	if(numCap<=myCapacity)//要扩大的容量必须大于现在容量
	{
		cerr<<"***The new vector's capacity is too small ***\n";
		exit(1);
	}
	else
	{
		ElementType *newArray;
		myCapacity=numCap;
		newArray=new(nothrow) ElementType[myCapacity]; //动态申请数组
		if(myArray==0)//内存不足报错
		{				
			cerr<<"***Inadequate memory to allocate vector***\n";
			exit(1);
		}
		for(int pos=0;pos<mySize;pos++)
			newArray[pos]=myArray[pos];//逐个赋值
		delete myArray; //销毁原数组
		myArray=newArray;
	}
		
	return *this;//返回对象本身
}

void Vector::push_back(const ElementType & value)//添加元素于尾部--------------------
{
	if (mySize == myCapacity)//如果向量已满,容量扩大一倍;
	    reserve(2*mySize);             
	   myArray[mySize] = value; //存入新元素     ;
       mySize++;       //有效数据个数+1;             	                        
}

void Vector::pop_back()//删除尾元素-----------------------
{
	if(empty())
	{
		cout<<"***The vector is empty ***\n";
		return;
	}
	else
		mySize--;
}

ElementType Vector::front()//返回第一个元素的引用----------------------
{
	if(!empty())
	return *myArray;
	else  
    {  
        cerr<<"***The vector is empty--returning a garbage value***\n";  
        ElementType garbage; 
        return garbage;   //返回垃圾值
	}
}

ElementType Vector::back()//返回最后一个元素的引用----------------------
{
	if(!empty())
	    return myArray[mySize-1]; 
    else  
    {  
        cerr<<"***The vector is empty--returning a garbage value***\n";  
        ElementType garbage;  //返回垃圾值
        return garbage;  
	}
}

ElementType & Vector::operator [] (int pos) const//下标访问(非越界检查)----------------
{
	return myArray[pos];
}

ElementType Vector::at(int pos)//下标访问(越界检查)----------------
{
    if(pos<0||pos>mySize-1)//判断是否越界
	{
		cerr<<"***Illegal location to visit---returning a garbage value***\n";
        ElementType garbage;  //返回垃圾值
        return garbage; 
	}
	return myArray[pos];
}

void Vector::swap(Vector & v1)//交换----------------------
{
    Vector temp=v1;
    v1=*this;       
    *this=temp;
}

bool Vector::operator==(const Vector & rightHandSide) const//值以及顺序的比较----------------
{
    assert(!empty());//保证数组非空
	assert(!rightHandSide.empty());//保证数组非空
    if(mySize==rightHandSide.mySize)
       for(int i=0;i<mySize;i++)//逐个赋值
        {
		    if(myArray[i]!=rightHandSide.myArray[i])//有一个元素不等时返回false
           return false;
		}
	else 
	return false;//元素数量不等时返回false
	return true;       
}

bool Vector::operator<(const Vector & rightHandSide) const//字典序比较-------------------
{
	assert(!empty());//保证数组非空
	assert(!rightHandSide.empty());//保证数组非空
	bool flag=true;
	int minSize=(mySize<rightHandSide.mySize?mySize:rightHandSide.mySize);//取最小size
       for(int i=0;i<minSize;i++)
        {
		    if(myArray[i]>=rightHandSide.myArray[i])//逐个比较
            flag=false;
		}
	if(!flag) 
	    return false;
	else
	{
		if(minSize==mySize)//判断前者是否元素数量最少
	    return true;//若是,返回true
		else
		return false;//若不是,返回false
	}	      
}

//----------------------------------新增功能-------------------------------------

void Vector::insert(int pos,ElementType item)//插入---------------------
{
	if(mySize==myCapacity)//容量已满
	{
		cerr<<"***No space for vector element ***\n";
		exit(1);
	}

	if(pos<0||pos>mySize)//非法位置插入
	{
		cerr<<"*** Illegal location to insert ***\n";
		return;
	}

	for(int i=mySize;i>pos;i--)//逐个赋值
		myArray[i]=myArray[i-1];

	myArray[pos]=item;//插入位置赋值
	mySize++;//元素个数加一
}

void Vector::erase(int pos)//删除-----------------------------
{
	if(mySize==0) //判断数组是否为空
    {  
        cerr<<"***List is empty.***\n";  
        return;  
    }  
    if(pos<0||pos>=mySize) //非法位置插入
    {  
        cerr<<"***Illegal location to delete***.\n";  
        return;  
    }  
    for(int i=pos;i<mySize;i++)  //逐个赋值
        myArray[i]=myArray[i+1];  
  
      mySize--; //元素个数减一
}

void Vector::display(ostream & out) const//显示所有元素-----------------------
{
	for(int i=0;i<mySize;i++)//逐个输出
		out<<myArray[i]<<" ";
	out<<endl;
}

ostream & operator <<(ostream & out,const Vector & aVector)//重载输出操作符-----------------
{
	aVector.display(out);//调用display函数
	return out;
}
//---------------------------------------------------------------------------------

//-----------------Vector_main.cpp------------------
#include<iostream>
using namespace std;

#include"Vector.h"

int main()
{
	Vector va;//定义一个不带参数的vector对象
	cout<<"The vector is created. Empty? "<<boolalpha<<va.empty()<<endl;//测试v.empty()
	cout<<"Using va.capacity() to output the capacity:"<<va.capacity()<<endl;//测试v.capacity()
	cout<<"Using va.size() to output the size:" <<va.size()<<endl<<endl;//测试v.size()

	Vector vb(0);//定义一个带一个参数(参数为0)的vector对象
	cout<<"The vector is created. Empty? "<<boolalpha<<vb.empty()<<endl;//测试v.empty()
	cout<<"Using vb.capacity() to output the capacity:"<<vb.capacity()<<endl;//测试v.capacity()
	cout<<"Using vb.size() to output the size:" <<vb.size()<<endl<<endl;//测试v.size()

	Vector vc(3);//定义一个带一个参数(参数不为0)的vector对象
	cout<<"The vector is created. Empty? "<<boolalpha<<vc.empty()<<endl;//测试v.empty()
	cout<<"Using vc.capacity() to output the capacity:"<<vc.capacity()<<endl;//测试v.capacity()
	cout<<"Using vc.size() to output the size:" <<vc.size()<<endl<<endl;//测试v.size()

	Vector v(3,1);//定义一个带两个参数的vector对象
	cout<<"The vector is created. Empty? "<<boolalpha<<v.empty()<<endl;//测试v.empty()
	cout<<"Using v.capacity() to output the capacity:"<<v.capacity()<<endl;//测试v.capacity()
	cout<<"Using v.size() to output the size:" <<v.size()<<endl<<endl;//测试v.size()

	cout<<"Using v.push_back() to put 2,3,4,5,6,7 to the back.\n";//测试v.push_back()
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);
	v.push_back(7);
	cout<<"After push back,the new size:"<<v.size()<<endl;//输出新的元素个数
	cout<<"After push back,the new capacity:"<<v.capacity()<<endl;//测试v.push_back()是否会引起容量变化
	
	cout<<"Using v.display(cout) to show the data:\n";v.display(cout);cout<<endl;//测试v.push_back()是否会引起容量变化
    cout<<"Using v.pop_back() to pop the back value:\n";v.pop_back();//下面会测试v.pop_back()
	cout<<"Using v.display(cout) to show the data:\n";v.display(cout);cout<<endl;
	cout<<"Using v.front() to show the front:";cout<<v.front()<<endl;//测试v.front()
	cout<<"Using v.back() to show the back:";cout<<v.back()<<endl;//测试v.back()以及v.pop_back()

	cout<<"Using v.insert(2,5) to insert a value to the vector:";v.insert(2,5);cout<<endl;//测试v.insert()
	cout<<"Using display(cout) to show the data:\n";v.display(cout);cout<<endl;
	cout<<"Using v.erase(7) to erase a value in the vector:";v.erase(8);cout<<endl;//测试v.erase()
	cout<<"Using display(cout) to show the data:\n";v.display(cout);cout<<endl;
	cout<<"Using v[6],v[7] to show the data:";cout<<v[6]<<" "<<v[7]<<endl;//测试v[]以及v.erase()
	cout<<"Using v.at(32) to check if out of range:\n";v.at(32);cout<<endl;测试v.at()
	
	Vector v1=v;//测试赋值函数
	cout<<"The new vector v1 is created:\n";v1.display(cout);//输出结果

	Vector v2(v);//测试复制构造函数
	cout<<"The new vector v2 is created:\n";v2.display(cout);//输出结果

	Vector v3(1,2);//使用push_back赋值
	v3.push_back(0);
	v3.push_back(1);
	v3.push_back(6);
	v3.push_back(2);
	v3.push_back(6);
	v3.push_back(8);
	v3.push_back(1);
	v3.push_back(1);
	v3.push_back(0);
	v3.push_back(1);
	v3.push_back(0);
	cout<<"Using display(cout) to show v3's data:\n";v3.display(cout);cout<<endl;//显示元素数据
	v3.swap(v);//测试v.swap()
	cout<<"After swap v3 and v,using display(cout) to show v's data:\n";//显示交换后的结果
	v.display(cout);//显示交换后v的结果
	cout<<"Using display(cout) to show v3's data:\n";
	v3.display(cout);//显示交换后v3的结果
	cout<<"Determine if v==v3 ? "<<boolalpha<<(v==v3)<<endl;//测试值以及顺序的比较
	cout<<"Determine if v<v3 ? "<<boolalpha<<(v<v3)<<endl;//测试字典序比较
	v.reserve(64);//测试v.reserve()
	cout<<"Using v.capacity() to output the capacity:"<<v.capacity()<<endl;//测试v.capacity()
	cout<<"Using v.size() to output the size:" <<v.size()<<endl;//测试v.size()

	system( "pause" );
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值