简体   繁体   中英

C++ - A 3-d data structure - should I use vector of pointers or vector of vectors?

I have to maintain a data structure which is 3 dimensional. So let us say it's dimensions are :- lxmx n. In the program that I wish to write l and m will be known since the time the data structure is being constructed . But n has to be dynamic throughout runtime. And n may be different for the different vectors in the grid lxm. (Once this structure is created I never intend to destruct it as I will be needing it throughout).

For the moment let us assume it's a 2-dimensional data structure that I wish to make, should I make a vector<vector<int>> or a vector<vector<int>*> ? Also I know how I might initialise the vector to the required size in the first case, ie I might do something like :-

vector<vector<int> > A(m)

to initialise the size of the outer dimension to m. But in the second case, once I have created a vector of pointers to vectors, how do I create the vectors to which the pointers point.

Carrying this to the 3-d case, should I use , a vector<vector<vector>> or a vector<vector<vector*>> or some other combination ?

Please, suggest any changes so that I may reframe the question if it's not framed properly.

You're better using a single vector (and not nested ones), since the memory is guaranteed to be contiguous in this case and your code will be way faster due to no cache misses. In this case, you need to map from 3D (2D) to 1D and vice-versa, but this is pretty straightforward

for 2D:

(x,y) <-> y*DIM_X + x;

for 3D:

(x,y,z) <-> z*(DIM_Y*DIM_X) + y*DIM_X + x;

If you really insist to use nested vectors, you can do something like:

#include <vector>

template<typename T>
using vec = std::vector<T>; // to save some typing

int main()
{
    // creates a 5 x 4 x 3 vector of vector of vector
    vec<vec<vec<double>>> v{5, vec<vec<double>>{4, vec<double>{3}}};
}

EDIT

Responding to your latest edit: use

std::vector<std::vector<double>> v{DIM_X*DIM_Y, std::vector<double>};
// address (x,y,z)
v[y*DIM_X+x, z] = val_x_y_z;

If you further happen to know the dimensions of the inner vectors, you can preallocate memory for them using std::vector::reserve . This will speed things up since there won't be any (slow) re-allocations.

No, use a proper multidimensional array: Boost MultiArray http://www.boost.org/doc/libs/1_59_0/libs/multi_array/doc/user.html

#include "boost/multi_array.hpp"

int main () {
  // Create a 3D array that is 3 x 4 x 2
  typedef boost::multi_array<int, 3> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[3][4][2]);

  // Assign values to the elements
  int values = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        A[i][j][k] = values++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM