Page 1 of 1
Vectors vs. Arrays
Posted: Thu Dec 11, 2008 4:17 pm
by Slacker
Which is better and why? Why or when would I need to use vectors?
I've only used various arrays throughout my programming career, and haven't really touched on vectors so I don't know much about them.
Re: Vectors vs. Arrays
Posted: Thu Dec 11, 2008 4:28 pm
by MarauderIIC
They're dynamically resizable arrays. You can also add/remove objects on the fly and have the vector resize to fit, so you don't waste space with a thousand '\0's or whatever. If the number of elements in your array will vary widely from the maximum or you don't know how many objects will be in your array or if you don't want to have a set maximum, you use a vector.
Re: Vectors vs. Arrays
Posted: Thu Dec 11, 2008 4:31 pm
by Falco Girgis
Don't be like most ignorant software developers who have never touched game programming in their life. Unfortunately the software development environment allows people to get away with being sloppy. Vectors are overkill usually.
They use vectors as if arrays don't exist. You use arrays 99% of the time. There will come a veeery rare occasion where you actually need a resizable array and won't know the maximum size--then chose a vector.
Re: Vectors vs. Arrays
Posted: Thu Dec 11, 2008 4:33 pm
by bugmenot
If you need the memory to be allocated off the stack, arrays. If you don't care, vectors. Just make sure you use the reserve function to prevent too many reallocations when you increase the vector size.
Edit: Slightly off topic but worth a read:
http://www.open-std.org/jtc1/sc22/wg21/ ... n2271.html
Re: Vectors vs. Arrays
Posted: Thu Dec 11, 2008 4:56 pm
by Slacker
Wow... that was a fast response.
GyroVorbis wrote:Don't be like most ignorant software developers who have never touched game programming in their life. Unfortunately the software development environment allows people to get away with being sloppy. Vectors are overkill usually.
They use vectors as if arrays don't exist. You use arrays 99% of the time. There will come a veeery rare occasion where you actually need a resizable array and won't know the maximum size--then chose a vector.
I figured this was the case, I assume similar to many C++ specific functions, where you forfeit speed and efficiency for safer or user-friendly functions.
Re: Vectors vs. Arrays
Posted: Thu Dec 11, 2008 5:16 pm
by bugmenot
Slacker wrote:I figured this was the case, I assume similar to many C++ specific functions, where you forfeit speed and efficiency for safer or user-friendly functions.
Actually, in release mode vector traversal is just as fast as arrays but provides safety checks in debug.
Read:
http://www.parashift.com/c++-faq-lite/c ... l#faq-34.1
http://www.gamedev.net/community/forums ... _id=513292
If you do want a 'raw' array but with some extra functionality, consider using
Boost's Array.