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的值,使其可以引用新数组数组