Array Vs. Vector

First, I apologize if this has been covered and if I am beating a dead horse. Second, I would like to thank you all in advance for any help you can provide in helping me understand.

I understand that arrays and vectors are similar. Their primary differences appear to be that vectors can dynamically change sizes where as arrays cannot. Also it seems that arrays COULD be limited to what data types they can store while vectors can store any data types (I may be mistaken on this). If all that is true, why not use vectors for everything? I presume there is a good reason for this I just don't fully understand it just yet.
Arrays and vectors can store the same data types, including each other and themselves.
Aside from arrays being faster than vectors, it's not unusual to have to interface with code that doesn't or can't use vectors, such as functions written in C.
If you want to decide whether to use an array, dynamic arrays, vectors or other containers depends on what you are going to do with them, each has its own pros and cons
Last edited on
it's not unusual to have to interface with code that doesn't or can't use vectors, such as functions written in C


So, it would seem good practice to avoid vectors unless you need them. Don't use them for all-purpse, correct?

dinamic arrays

Dynamic arrays exist outside of vectors? I was not aware...
If you use C++ is nothing wrong in using STL

With pointers you can dynamically allocate and deallocate arrays.
The problem with this is that unless you manually copy all the members from the old array into the new one, you will lose them
http://www.cplusplus.com/doc/tutorial/dynamic.html
So, it would seem good practice to avoid vectors unless you need them. Don't use them for all-purpse, correct?
Well, nothing is really all-purpose in programming.
Vectors are general-purpose, which means that generally it doesn't make a difference using one or the other, but that sometimes it does.
I won't tell you to avoid vectors, because that's just silly. If you prefer vectors and there's no technical reason to use one or the other, then go ahead and use vectors.
If you use choose to use arrays, keep in mind that you have to deal with memory management, and some people just aren't very good at it.

Bazzy is talking about dynamically allocated arrays.
I have read that arrays might be a little more efficient with memory usage. I also heard that arrays can be better performance.

Are either of these statements true? If yes, are they true enough to have any bearing on usage?
Last edited on
Arrays are not faster than vectors. Vectors are fully compatible with arrays. There is, however, an overhead in memory usage - which does matter if and only if you have very very many vectors. Vectors are recommended as the substitution for C-style arrays - use them as such. (One simple reason is that you will, at some point, realize that you need to know the size of your array. Then you are screwed if you didn't use vectors in the first place. Another reason is the clean way of handling all containers in the same way - when using a vector::const_iterator it is written in exactly the same way as a list::const_iterator, but for an array things are different. So when changing the container type, you can either change all your source code, or just the container, if you were using a vector and it's common interface. In short, preferring vectors over arrays gives you many advantages for very little extra (memory) cost)
Thank you all for your input. I think I have a better grasp now.
Topic archived. No new replies allowed.