Jump to content
  • Advertisement
Sign in to follow this  
Sevans

2D Vector?

This topic is 4443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there such a thing as a 2D vector w/o declaring a vector of vectors, or an array of vectors? I would like to pass this 2D vector by reference, but I cant find the syntax to do this for an array of vectors, and a vector of vectors just seems like too much overhead. Thanks, -sevans

Share this post


Link to post
Share on other sites
Advertisement
It is not too bad:

vector<vector<int> > v; // note the space between the '>'s.

If you are going to be using it a lot or passing it around, you can do:

typedef vector<vector<int> > 2DVector;

then just do:

void f(2DVector &V)
{
cout << V[2][3] << endl;
}

or whatever.

Share this post


Link to post
Share on other sites
Note that EasilyConfused's code allows a 2D vector to be "jagged": Each internal 1D vector is able to have a size independent of the others. So you won't be able to say, "This is a 10 by 12 2D vector" unless you've manually made sure during creation that all the internal vectors are the same size, and that if they ever get resized, all of them get resized to the same value.

Sometimes this is actually good, but usually it is irrelevant, and sometimes complicates things. Also, this method of a 2D vector ends up having a bunch of chunks of memory allocated, rather than one single chunk like an plain old multidimensional array would.

One alternative would be to simply have a 1D vector, but when accessing it, calculate the indices yourself. If using basic row/column indices, the overall 1D vector size would be (width * height), and any particular element would be located at (row * width + column).

Finally, probably the best solution, if you can be bothered to get it, would be to use Boost.MultiArray.

Share this post


Link to post
Share on other sites
0) If you need it to be rectangular, and resizeable in both directions, use Boost::multi_array.

1) If you need it to NOT be rectangular (the third case is "don't require either"; but I really don't see how that would be possible), just use a vector of vectors. It's not really that much overhead - actually, if you need the non-rectanguarlity, it's basically as little overhead as you can sanely get away with.

2) If you need it to be rectangular and NOT resizeable in either direction, you can just use a 2D array - but passing around arrays is very tricky stuff. I would recomment making a boost::array of boost::arrays of whatever. (Note that this boost::array is different from - quite simpler and less powerful than boost::multi_array.)

3) If you need it to be rectangular and resizeable only in the FIRST direction, then you don't want an array of vectors, but instead a vector of arrays. Again, you can't just use an array as a storage type (you'd get pointers instead); use a vector of boost::arrays.

4) If you need it to be rectangular and resizeable only in the SECOND direction, then, as you might expect, use a boost::array of vectors. An ordinary array might be ok.

5) If you really, really want to do things yourself and probably get them wrong (but hopefully learn something in the process), read this.

Share this post


Link to post
Share on other sites
Thanks guys, a vector of vectors as a typdef is my choice now, just for ease since this is such a small simple program.

-sevans

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!