template<class T>
void changeLength1D(T*& a,int oldLength,int newLength)
{
if(newLength >= 0)
{
T* temp = new T[newLength];
int number = min(oldLength,newLength);//确定要复制元素个数
copy(a,a+number,temp);
delete [] a;//释放老数组空间
a = temp;
}
else
{
cout<<"数组长度应该大于0"<<endl;
}
}
该函数的大致思路是: 1.建立一个具有新长度的数组 2.把数组a的元素复制到这个新数组 3.改变数组a的值,使其能够引用新数组