How to allocate a contiguous two dimension vector?

 
vector<vector<int> > twoDim(20, vector<int>(20, 0));


vector of vector is ok but the array is ragged
I only know two way to do it
1 : define a data structure by myself("new" the memory by myself)
2 : use boost::multi_array
3 : simulate one dimension vector as two dimension vector
1
2
vector<int> A(row * col);//.... do something
      A(2 * col + size )


About the first choice, I don't want to define my data structure
if the standard library could do it for me, so I pass it for now

The second choice, not everyone know boost especially if you were
not at the right place like me(I am an EE student, an amateur of
C++ like the other EE students).

The third choice, the syntax may not so intuitive
Do you have any ideas?Thanks a lot
I think I'm correct in saying that solution 3 is the only one which will guarantee that your 2D vector is both rectangular (well, sort of. It depends on interpretation and if you decide to append data to increase the size of the vector or not) as well as in a contiguous block of memory (The boost::multi-array may also have the same properties, I've never looked into using it myself).

There's nothing wrong with your original solution. each row will be contiguous, but separate rows will be found in different locations in memory. For most applications I don't see this being a problem. Again, this could run into certain problems if you decide to append items onto any of the vectors, but if you don't, then there's nothing which makes it so you can't have a n*m rectangular matrix.

If you know that the size of your array isn't going to change between runs, you could just hard-code in the size into a 2D array.
Thanks, I don't need to "push" anything during the runtime, the size would be determine from the beginning.

Looks like solution 3 maybe the most easiest way to do it for now
I am wonder why don't C++0x attend to add multi_array like boost did?

Solution 1, Maybe this is a good chance to practice how to work with unique_ptr and move semantic
Hopefully C++ will add the contiguous multi_array like boost did in the future, this would solve a lot of compatible problems
Topic archived. No new replies allowed.