/* * 數組內元素的位置是不變的,只是數組容量的大小發生了變化。 * 簡單來講,就是將數組的左上角元素平移到新數組的相同位置。 * */ template<class T> void changeLength2D(T**& a,int oldRows int copyRows,int copyColumns, int newRows,int newColumns) { if(copyRows > newRows || copyColumns > newColumns) throw illegalParameterValue("new dimensions are too small"); T** temp = new T*[newRows]; for(int i = 0;i < newRows;i++) temp[i] = new T[newColumns]; //將元素從舊空間遷移到新空間,並釋放舊空間 for(int i = 0;i< copyColumns;i++) { copy(a[i],a[i]+copyColumns,temp[i]); delete [] a[i]; } for(int i = copyRows;i< oldRows;i++) delete [] a[i]; delete [] a; a = temp; }