Sign in to follow this  
lonewolff

Two dimensional array question

Recommended Posts

Hi Guys,

 

Just wondering if it is possible to somehow create and initialise a 2D array with varying sized data.

 

Something like this;

 

float item[][0] = { 1.0, 9.0, 3.5 };
float item[][1] = { 9.5, 1.2, 9.9, 7.7, 2.4, 1.8 };

 

And then access the data how ever I wise after that, being aware to check sizeof(item[x][y]) obviously.

 

Is this sort of thing possible?

 

Or is a std::vector better for this sort of thing?

 

Thanks in advance.

 

 

 

 

 

Share this post


Link to post
Share on other sites

Take a step back... what are you trying to achieve? 

 

Will the size of the individual rows change, or will it be allocated upfront? You could make it a 1d array with some clever indexing (hidden behind a class).

 

There are lots of ways of doing this... in terms of simplicity a vector of vectors (std::vector<std::vector<float>>) will achieve what you want with a minimum of fuss and be almost guaranteed to be leak free. But it might not be optimal for your scenario (if performance is a consideration).

Share this post


Link to post
Share on other sites

Hi there and thanks for the reply.

 

Each array will be static. Essentially, I want to store frame animation and each frame is a 1D array. So, I want to be able to increment or manually choose what frame I want but have it in the form of frame[n]. 'n' being the frame number.

Share this post


Link to post
Share on other sites

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