Let me preface by saying I have extensive experience working with Python.
I am looking to build the equivalent of a NumPy array (Python) in C++.
My issue is trying to create a Multidimensional array in C++ in such a manner that the number of dimensions isn't specified within the array creation method, but provided as an argument to this method.
For example, I want to be able to do something like this:
struct Array {
Array(int ndim, int dim1_size, int dim2_size......) {
// ndim specifies the number of dimensions in array
// ndim = 4 --> arr = Array[3][4][2][5]
// ndim = 2 --> arr = Array[5][8]
......
}
What is the most elegant way to do this? I know one solution is to add the elements to a flattened 1D array, but this makes matrix multiplication etc difficult for me to implement. I would prefer to preserve the shape of the array, to keep my understanding of matrix operations as organic as possible.
Maybe a pointer of pointers solution? But I am not quite sure how to formulate the code for that.
I agree about using flattened 1d array is best in this case in conjunction with variadic templated class/operator() so that the number of dimensions/dimension size can be specified for the constructor and the indexes for operator().