lundi 30 décembre 2019

Initialize a 2d array with unknown first dimension size in C++

Say I need a 2d array, first dimension size set at runtime, and second dimension size is set to 5 at compilation time.

Since we can do this to initialize a 1d array with unknown size

int* arr;
arr = new int[5];

I would like to make the following code work

int* arr[5];
arr = new int[12][5];

Notice:

I need the second dimension set to 5, not first dimension. So I need to be able to do arr[12][5] but not arr[5][12].

I know I can make arr an int** and then assign a 2d array to arr, so please avoid such answer.

I know I can use STL containers such as vector, so please avoid such answer.

Aucun commentaire:

Enregistrer un commentaire