using namespace std;
class outofbount{};
class illegalsize{};
template<class elemtype>
class seqlist //:public list<elemtype>
{
private:
elemtype*date;
int currentlength;
int maxsize;
void doublespace();
public:
seqlist(int initsize=10);
~seqlist(){delete[]date;}
int length()const{return currentlength;}
void add(const elemtype&x);
void print(){for (int i=0;i<this->currentlength;i++) cout<<this->date[i]<<' ';}
seqlist operator+( /*const seqlist &a,*/ const seqlist &b);
seqlist &operator=(const seqlist &b);
};
template<class elemtype>
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
date=new elemtype[initsize];
maxsize=initsize;
currentlength=0;
}
template<class elemtype>
void seqlist<elemtype>::doublespace()
{elemtype*tmp=date;
maxsize*=2;
date=new elemtype[maxsize];
for(int i=0;i<currentlength;++i) date[i]=tmp[i];
delete[]tmp;}
template<class elemtype>
void seqlist<elemtype>::add(const elemtype &x){
if (currentlength==maxsize)
doublespace();
this->date[currentlength++]=x;
}
template<class elemtype>
seqlist<elemtype> seqlist<elemtype>::operator+(const seqlist<elemtype> &b)
{int alen=this->currentlength;
int blen=b.currentlength;
seqlist<elemtype> c(alen+blen+2);
for (int i=0;i<alen;i++){ c.date[i]=this->date[i];c.currentlength++;}
for (int i=0;i<blen;i++){c.date[i+alen]=b.date[i];c.currentlength++;}
return c;}
template<class elemtype>
seqlist<elemtype> &seqlist<elemtype>::operator=(const seqlist<elemtype> &b)
{ if(this==&b) return *this;
this->currentlength=0;
int asiz=this->maxsize;int blen=b.currentlength;
while(asiz<=blen)this->doublespace();
for(int i=0;i<blen;i++)this->date[i]=b->date[i];
this->currentlength=b.currentlength;
return *this;
}
int num(const char * type)
{if (type[0]=='i') return 0;if(type[0]=='d') return 1;if(type[0]=='c')return 2;return -1;}
int main(){
char type[2000];int n,m;
cin>>type;
cin>>n;
cin>>m;
switch (num(type)){
case 0:{
seqlist<int>v(n),u(m);
for(int i=0;i<n;i++)
{int a;cin>>a;v.add(a);}
for(int i=0;i<m;i++)
{int a;cin>>a;u.add(a);}
seqlist<int>q=v+u;q.print();break;}
case 1:{seqlist<double>v(n),u(m);
for(int i=0;i<n;i++)
{double a;cin>>a;v.add(a);}
for(int i=0;i<m;i++)
{double a;cin>>a;u.add(a);}
seqlist<double>q=v+u;q.print();break;}
case 2:{seqlist<char>v(n),u(m);
for(int i=0;i<n;i++)
{char a;cin>>a;v.add(a);}
for(int i=0;i<m;i++)
{char a;cin>>a;u.add(a);}
seqlist<char>q=v+u;q.print();break;}}
return 0;
}
6 个解决方案
#1
你还需要写一个复制构造函数
#2
我写了啊template<class elemtype>
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
date=new elemtype[initsize];
maxsize=initsize;
currentlength=0;
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
date=new elemtype[initsize];
maxsize=initsize;
currentlength=0;
#3
我写了啊template<class elemtype>
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
date=new elemtype[initsize];
maxsize=initsize;
currentlength=0;
#4
请搜索 拷贝构造函数
应该是这样的:
seqlist<elemtype>::seqlist(const seqlist<elemtype>&list){
...
}
#5
你写的那个不是复制构造函数。应该要这样:
template<class elemtype>
seqlist<elemtype>::seqlist(const seqlist<elemtype>& other)
{
//...
}
#6
gcc默认是返回值优化, 所以看不出来, 这应该需要move的构造,
seqlist(seqlist && r)
#1
你还需要写一个复制构造函数
#2
我写了啊template<class elemtype>
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
date=new elemtype[initsize];
maxsize=initsize;
currentlength=0;
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
date=new elemtype[initsize];
maxsize=initsize;
currentlength=0;
#3
你还需要写一个复制构造函数
我写了啊template<class elemtype>
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
date=new elemtype[initsize];
maxsize=initsize;
currentlength=0;
#4
我写了啊template<class elemtype>
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
date=new elemtype[initsize];
maxsize=initsize;
currentlength=0;
你还需要写一个复制构造函数
我写了啊template<class elemtype>
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
date=new elemtype[initsize];
maxsize=initsize;
currentlength=0;
请搜索 拷贝构造函数
应该是这样的:
seqlist<elemtype>::seqlist(const seqlist<elemtype>&list){
...
}
#5
你写的那个不是复制构造函数。应该要这样:
template<class elemtype>
seqlist<elemtype>::seqlist(const seqlist<elemtype>& other)
{
//...
}
#6
gcc默认是返回值优化, 所以看不出来, 这应该需要move的构造,
seqlist(seqlist && r)