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.
Vectors vs. Arrays
Moderator: Coders of Rage
- Slacker
- Chaos Rift Newbie
- Posts: 31
- Joined: Tue Nov 11, 2008 11:41 am
- Location: MountLake Terrace, WA
- Contact:
Vectors vs. Arrays
~Slacker
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Vectors vs. Arrays
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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Vectors vs. Arrays
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.
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
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
Edit: Slightly off topic but worth a read: http://www.open-std.org/jtc1/sc22/wg21/ ... n2271.html
- Slacker
- Chaos Rift Newbie
- Posts: 31
- Joined: Tue Nov 11, 2008 11:41 am
- Location: MountLake Terrace, WA
- Contact:
Re: Vectors vs. Arrays
Wow... that was a fast response.
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.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.
~Slacker
Re: Vectors vs. Arrays
Actually, in release mode vector traversal is just as fast as arrays but provides safety checks in debug.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.
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.