次のようなnewを使用して2D配列を作成したとします。
int **arr = new int*[rows];
for(int i=0; i<rows; ++i)
arr[i] = new int[cols];
次に、サイズを変更するには、次のような操作を行う必要があります。
int newRows = rows/2;
// Create a new array for the right number of rows.
int **newArr = new int*[newRows];
// Copy the rows we're keeping across.
for(int i=0; i<newRows; ++i)
newArr[i] = arr[i];
// Delete the rows we no longer need.
for(int i=newRows; i<rows; ++i)
delete[] arr[i];
// Delete the old array.
delete[] arr;
// Replace the old array and row count with the new ones.
arr = newArr;
rows = newRows;
しかし、真剣に、これはベクトルを使用するだけで非常に簡単です。
std::vector<std::vector<int>> v(rows);
for(int i=0; i<rows; ++i)
v[i].resize(cols);
v.resize(v.size()/2);