I'm trying to define a dynamic 2D Array in C++ using the following definition:
int foo(string parameter){
const int n = parameter.length();
int* Array = new int[n][n];
return 0;
}
I receive an error that array size in new expression must be constant, can't understand why because Array
is supposed to be dynamic.
std::vector<std::vector>
. To fix your code you canint* Array = new int[n * n];
and access the elements withArray[row + n * col]
.