二维变长数组
2019獨角獸企業重金招聘Python工程師標準>>>
/** 數組內元素的位置是不變的,只是數組容量的大小發生了變化。* 簡單來說,就是將數組的左上角元素平移到新數組的相同位置。* */ template<class T> void changeLength2D(T**& a,int oldRowsint 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; }轉載于:https://my.oschina.net/u/1771419/blog/1799510
總結
- 上一篇: php解释命令行的参数
- 下一篇: 【sublime text3】破解 最近