Reputation: 7408
all
I am using multidimensional STL vector to store my data in C++. What I have is a 3D vector
vector<vector<vector<double>>> vec;
What I want to retrieve from it is :
&vec[][1][]; // I need a pointer that points to a 2D matrix located at column 1 in vec
Anyone has any idea to do so? I would be extremly appreciate any help!
Regards
Long
Upvotes: 0
Views: 1883
Reputation: 567
Consider using matrix from some linear algebra library. There are some directions here
Upvotes: 0
Reputation: 61222
It is best to consider vec
just as a vector whose elements happen to be
vectors-of-vectors-of-double, rather than as a multi-dimensional structure.
You probably know, but just in case you don't I'll mention it,
that this datatype does not necessarily represent a rectangular cuboid.
vec
will only have that "shape" if you ensure that all the vectors are
the same size at each level. The datatype is quite happy for the vector vec[j]
to be a different size from the one at vec[k]
and likewise for vec[j][n]
to be a vector of different size from vec[j][m]
, so that your structure is "jagged".
So you want to get a pointer to the vector<vector<double>>
that is at
index 1 in vec
. You can do that by:
vector<vector<double>> * pmatrix = &vec[1];
However this pointer will be an unnecessarily awkward means of accessing that
vector<vector<double>>
. You certainly won't be able to write the
like of:
double d = pmatrix[j][k];
and expect to get a double
at coordinates (j,k) in the "matrix addressed
by a pmatrix
". Because pmatrix
is a pointer-to-a-vector-of-vector-of-double;
so what pmatrix[j]
refers to is the vector-of-vector-of-double (not vector-of-double)
at index j
from pmatrix
, where the index goes in steps of
sizeof(vector<vector<double>>)
. The statement will reference who-knows-what
memory and very likely crash your program.
Instead, you must write the like of:
double d = (*pmatrix)[j][k];
where (*pmatrix)
gives you the vector-of-vector-of-double addressed by pmatrix
,
or equivalently but more confusingly:
double d = pmatrix[0][j][k];
Much simpler - and therefore, the natural C++ way - is to take a reference,
rather than pointer, to the vector<vector<double>>
at index 1 in vec
. You
do that simply by:
vector<vector<double>> & matrix = vec[1];
Now matrix
is simply another name for the vector<vector<double>>
at index 1 in vec
,
and you can handle it matrix-wise just as you'd expect (always assuming
you have made sure it is a matrix, and not a jagged array).
Another thing to consider was raised in a comment by manu343726. Do you
want the code that receives this reference to vec[1]
to be able to
use it to modify the contents of vec[1]
- which would include changing its
size or the size of any of the vector<double>
s within it?
If you allow modification, that's fine. If you don't then you want to get a const reference. You can do that by:
vector<vector<double> > const & matrix = vec[1];
Possibly, you want the receiving code to be able to modify the double
s
but not the sizes of the vectors that contain them? In that case, std::vector
is the wrong container type for your application. If that's your position I
can update this answer to offer alternative containers.
Upvotes: 1