If you need a multidimensional array, you can use pointers; resizing will require copying to a new one and deleting the old one, but you can do the following:
int** m;
int rows, cols;
cin >> rows >> cols;
m = new int* [rows];
for (int i = 0; i < rows; i++) {
m[i] = new int [cols];
}
for (int i = 0; i < rows; i++) {
delete [] m[i];
}
delete [] m;
or alternatively you can use a pointer to a 1D array, for example:
int* m;
int rows, cols;
cin >> rows >> cols;
m = new int [rows*cols];
and access it:
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
m[i*cols+j] = i;
containing the delete statement:
delete [] m;
user3105379
source
share