• Advertisement
Sign in to follow this  

question about arrays and vectors

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

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
Advertisement
Yep, although you can just use the vector subscript rather than at() if you want (and you don't want range checking, of course):


cout << vArrayVector[y][x] << endl;

Share this post


Link to post
Share on other sites
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
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
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
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
Sign in to follow this  

  • Advertisement