Sign in to follow this  

Vector to hold 2d int Array

This topic is 4382 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

Hi, i am currently writing a game Ai for which i am generating moves and storing then in a dynamic structure,thus the choice of vector. Initially when i started out ,1D looked great, but after changes i have decided to go with 2D representation. Creating a vector which just holds ints is a breeze vector <int> move_list; but since the approach has changed , i would like this to store a 2D integer array.i would like to know if there is a support for the same by the library or i need to encapsulate the moves in a structure , create a vector of that and then parse the values from it. Cheers! [Edited by - Nokturnal on December 16, 2005 5:13:21 PM]

Share this post


Link to post
Share on other sites
Just as a quick note, you could always just use a vector of vectors to emulate the 2D storage. I don't know about performance of this but I don't see it being a bad thing. Just use the original vector for either your x or y axis and then the element vectors are the other axis.

Something like this:
vector < vector <int> > my_variable;

Share this post


Link to post
Share on other sites
Boost::multi_array is probably the best standard option.

If you really want to do things yourself, you should read this (and the next few sections). (Actually, beginning programmers would do well to read the whole C++ FAQ Lite :) )

Share this post


Link to post
Share on other sites
From what I know, other than a vector<vector> approach (or some other nested container) or a [][] array, there isn't any built in support for it. You could use a single [] array for the job. You could do something like:

gridSize = width * height;
elements = new int[gridSize];

and then to access something at a given x,y location, you would access it like such:

elements[x + y*width];

which of course can be wrapped in a function with some bounds checking.

The reason you do the y*width is because each row is width elements wide. If you want to skip the first line (where the first line is 0 and the line you want access to is y=1) that's 'width' elements you skip. This will "bump" you down the rows so you can move across the given row.

This is a simple solution (like the nested vector) using the features of the language. I'm not familiar with Boost myself (I've wanted to for a bit) but I've heard good things about it as well so it might be worth checking out.

Shawn

PS - I make no guarantee's about anything I say since I said I would start participating in hopes that my talking will further enforce what I do and do not know about. If I make a mistake, someone will hopefully kick my butt and educate me as to the correct response.

Share this post


Link to post
Share on other sites

This topic is 4382 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this