Sign in to follow this  
rherm23

question about arrays and vectors

Recommended Posts

rherm23    147
I have a question. I was wondering about the following i have 3 arrays of ints. I have a vector in which i would like to store the arrays. how would i access the elements of the arrays .. would i do something like .. cout << vArrayVector.at(y)[x] << endl;

Share this post


Link to post
Share on other sites
Zahlman    1682
You can't really make a vector of arrays, because arrays aren't a "first-class type" - they're an awful hack is what they are ;) You can handle this by making a struct wrapper for them, or using a vector of vectors (if the "arrays" are different lengths), or by using an *array* of vectors. (After all, you *do* know the number of "arrays" - there are 3 of them - so *that's* the dimension that's a strong candidate for representation with an array rather than a vector.)

Share this post


Link to post
Share on other sites
DrEvil    1148
If you're using MSVC 2005 even indexing with [] does checks at runtime that result in it being significantly slower than 2003. There are #defines to disable it, but I don't remember them offhand.

Share this post


Link to post
Share on other sites
TheAdmiral    1122
Quote:
Original post by DrEvil
If you're using MSVC 2005 even indexing with [] does checks at runtime that result in it being significantly slower than 2003. There are #defines to disable it, but I don't remember them offhand.

Surely this can't apply to release builds..?

Admiral

Share this post


Link to post
Share on other sites
SunTzu    286
I understand that it does apply to Release builds, though I've not yet got 2005 so I'm not certain about that. I believe the directive to disable it is HAS_ITERATOR_DEBUGGING, though the same caveat applies.

Back to the original question... while vArrayVector[y][x] (or using at()) might be syntactically correct, it's almost certain not to work... unless the vector stores pointers to the arrays I guess... so you're better off making them a struct, or having an array of arrays, or even better an array of vectors. (Which, reading back, I see Zahlman has already said).

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