question about arrays and vectors
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;
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;
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.)
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.
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
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).
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement