Vectors vs. Arrays

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Slacker
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 31
Joined: Tue Nov 11, 2008 11:41 am
Location: MountLake Terrace, WA
Contact:

Vectors vs. Arrays

Post 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.
~Slacker
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Vectors vs. Arrays

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post 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.
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: Vectors vs. Arrays

Post 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
User avatar
Slacker
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 31
Joined: Tue Nov 11, 2008 11:41 am
Location: MountLake Terrace, WA
Contact:

Re: Vectors vs. Arrays

Post 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.
~Slacker
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: Vectors vs. Arrays

Post 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.
Post Reply