std::vector and contiguous memory of multidimensional arrays

The standard does require the memory of an std::vector to be
contiguous. On the other hand, if you write something like:

std::vector<std::vector<double> > v;

the global memory (all of the v[i][j]) will not be contiguous. The
usual way of creating 2D arrays is to use a single

std::vector<double> v;

and calculate the indexes, exactly as you suggest doing with float.
(You can also create a second std::vector<float*> with the addresses
if you want. I’ve always just recalculated the indexes, however.)

Leave a Comment