//ArrayList.h类的声明
class ArrayList{
private :
int *data;//数据保存的地方
int size;//数据的数量
int capacity;//数据的容量
public:
ArrayList();//默认构造函数,构造一个空的类
ArrayList(const ArrayList&rhs);//拷贝函数
ArrayList(int n,int value);//填充构造函数,构造一个内容为n个value的顺序表
~ArrayList();//析构函数,一定要自己实现,否则会有内存泄漏
int getSize()const{return size;}//返回数据size的函数
//ArrayList类增删查改的函数
void insert(int pos ,int value);
void remove(int pos);
int at(int pos);
void modify(int pos,int newValue);
void disp()const;
void setCapacity(int newCapa);
//赋值运算符的重载
ArrayList& operator =(const ArrayList&rhs);
ArrayList& operator +=(const ArrayList&rhs);
int& operator [](int pos);//返回引用
const int& operator [](int pos)const;//不返回引用
const ArrayList operator +(const ArrayList&lhs,const ArrayList&rhs);//加号运算符重载
//关系运算符重载,按照字典序比较顺序表
bool operator ==(const ArrayList&lhs,const ArrayList&rhs);
bool operator !=(const ArrayList&lhs,const ArrayList&rhs);
bool operator <(const ArrayList&lhs,const ArrayList&rhs);
bool operator <=(const ArrayList&lhs,const ArrayList&rhs);
bool operator >(const ArrayList&lhs,const ArrayList&rhs);
bool operator >=(const ArrayList&lhs,const ArrayList&rhs);
//流输出运算符非成员函数的重载
using std::ostream;
ostream& operator <<(ostream&os,const ArrayList&rhs);
};
//ArrayList.cpp (ArrayList.h中函数的具体实现)
ArrayList:: ArrayList()
{capacity=1;
data=new int [capacity];
data=0;
size=NULL;
}
ArrayList:: ArrayList(const ArrayList&rhs)
{
data=new int [rhs.capacity];
capacity=rhs.capacity;
size=rhs.size;
for(int i=0;i { data[i]=rhs.data[i];} } ArrayList:: ArrayList(int const a[],int n) { data=new int [n]; for(int i=0;i {data[i]=a[i];} size=n; capacity=rhs.capacity; } ArrayList::ArrayList(int n,int value) { data=new int [n]; for(int i=0;i {data[i]=value;} size=n; capacity=n; } ArrayList::~ArrayList() void ArrayList::setCapacity(int newCapa) { if(newCapa<=capacity) return ; int *pt=new int [capacity=newCapa]; for(int i=0;i {pt[i]=data[i];} delete[]data; data=pt; } void ArrayList::insert(int pos,int value) if(size==capacity) {setCapacity(2*capacity);} for(int i=size-1;i>=pos;--i) {data[i+1]=data[i];} data[pos]=value; ++size; } void ArrayList::remove(int pos) { for(int i=pos;i {data[i]=data[i+1];} --size; } int ArrayList::at(int pos)const data[pos]=newValue; } void ArrayList::disp()const { for(int i=0;i size=rhs.size; capacity=rhs.capacity; for(int i=0;i { data[i]=rhs.data[i];} } ArrayList& ArrayList::operator += (const ArrayList&rhs) int & ArrayList::operator[](int pos) const int & ArrayList::operator[](int pos) const const ArrayList operator + (const ArrayList&lhs,const ArrayList&rhs) bool operator == (const ArrayList&lhs,const ArrayList&rhs) if (lhs.getSize() == rhs.getSize()) { bool operator != (const ArrayList&lhs,const ArrayList&rhs) } bool operator < (const ArrayList&lhs,const ArrayList&rhs) bool operator > (const ArrayList&lhs,const ArrayList&rhs) bool operator >= (const ArrayList&lhs,const ArrayList&rhs) using std::ostream; } }
{};
{
{
return data[pos];
}
void ArrayList::modify(int pos,int newValue)
{
}
ArrayList& ArrayList::operator = (const ArrayList&rhs)
{
{ int pos=size;
setCapacity(capacity+rhs.capacity);
for(int i=0;i
this->insert(pos,rhs[i]);
++pos;
}
}
{
return data[pos];
}
{
return (const_cast
}
{
int size=lhs.getSize();
ArrayList temp(lhs);
auto pos=temp.getSize();
for(int i=0;i
temp.insert(pos,rhs[i]);
++pos;
}
return temp;
}
{
for (int i = 0; i < lhs.getSize(); i++)
{
if (lhs[i] != rhs[i])
{ return false;}
}
return true;
}
else
{
return false;
}
}
{
if(lhs==rhs)
return 0;
else return 1;
{
auto len = std::min(lhs.getSize(), rhs.getSize());
for (int i = 0; i < len; i++)
{
if (lhs[i] < rhs[i])
{
return true;
}
if (lhs[i] > rhs[i])
{
return false;
}
}
if (lhs.getSize() > rhs.getSize())
{
return false;
}
if (lhs.getSize() < rhs.getSize())
{
return true;
}
return false;
}
bool operator <= (const ArrayList&lhs,const ArrayList&rhs)
{
if(lhs==rhs||lhs
else return 0;
}
{
if (lhs<=rhs)
return 0;
else
return 1;
}
{
if(lhs
else return 1;
}
ostream& operator << (ostream&os,const ArrayList&rhs)
{
for(int i=0;i
os<
return os;