Vector容器的简单实现
最新推荐文章于 2024-10-27 18:53:46 发布
原创
最新推荐文章于 2024-10-27 18:53:46 发布
·
1.4k 阅读
·
3
·
11
·
CC 4.0 BY-SA版权
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
数据结构
专栏收录该内容
25 篇文章
订阅专栏
本文介绍了一个名为miniVector的类的设计与实现过程,该类实现了数组大小可调、元素的增删改查等功能,并提供了复制构造、赋值、扩容等方法。通过具体的测试案例验证了各成员函数的正确性。
/*设计并实现完整的miniVector类,要求增加插入和删除函数,并测试主要的成员函数。
miniVector的基本功能应该包括以下几项:数组大小可调,
允许对任一元素赋值,修改,增加,删除并且不影响其他元素的正常访问。
扩展功能应包括允许通过下标访问,允许将一个对象复制给另一个对象,和其他功能。*/
//----------------------------------Vector.h---------------------------------------
#include
#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
#include
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 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 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 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 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 { 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 for(int i=0;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 myArray[i]=myArray[i+1]; mySize--; //元素个数减一 } void Vector::display(ostream & out) const//显示所有元素----------------------- { for(int i=0;i out< out< } ostream & operator <<(ostream & out,const Vector & aVector)//重载输出操作符----------------- { aVector.display(out);//调用display函数 return out; } //--------------------------------------------------------------------------------- //-----------------Vector_main.cpp------------------ #include using namespace std; #include"Vector.h" int main() { Vector va;//定义一个不带参数的vector对象 cout<<"The vector is created. Empty? "< cout<<"Using va.capacity() to output the capacity:"< cout<<"Using va.size() to output the size:" < Vector vb(0);//定义一个带一个参数(参数为0)的vector对象 cout<<"The vector is created. Empty? "< cout<<"Using vb.capacity() to output the capacity:"< cout<<"Using vb.size() to output the size:" < Vector vc(3);//定义一个带一个参数(参数不为0)的vector对象 cout<<"The vector is created. Empty? "< cout<<"Using vc.capacity() to output the capacity:"< cout<<"Using vc.size() to output the size:" < Vector v(3,1);//定义一个带两个参数的vector对象 cout<<"The vector is created. Empty? "< cout<<"Using v.capacity() to output the capacity:"< cout<<"Using v.size() to output the 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:"< cout<<"After push back,the new capacity:"< cout<<"Using v.display(cout) to show the data:\n";v.display(cout);cout< 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< cout<<"Using v.front() to show the front:";cout< cout<<"Using v.back() to show the back:";cout< cout<<"Using v.insert(2,5) to insert a value to the vector:";v.insert(2,5);cout< cout<<"Using display(cout) to show the data:\n";v.display(cout);cout< cout<<"Using v.erase(7) to erase a value in the vector:";v.erase(8);cout< cout<<"Using display(cout) to show the data:\n";v.display(cout);cout< cout<<"Using v[6],v[7] to show the data:";cout< cout<<"Using v.at(32) to check if out of range:\n";v.at(32);cout< 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< 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 ? "< cout<<"Determine if v v.reserve(64);//测试v.reserve() cout<<"Using v.capacity() to output the capacity:"< cout<<"Using v.size() to output the size:" < system( "pause" ); return 0; }