Possible Duplicate:
C++ STL vector vs array in the real world
To start of I have a little below average knowledge of C++ and advanced knowledge of C
Many times, I want to ask something code related and include or refer to some code and an array comes up everyone instantly lock on this aspect and suggest I should instead use a vector, even if it doesn’t have to do anything related to my problem.
The problem is that this is the way I learned how to use C++, obviously something I inherited from learning plain C first, and I am quite used to it, although I feel confident about using vectors also, I just prefer arrays over vectors.
My questions are why should someone use a vector instead of an array, what does he earn by doing so, even if he is quite used to arrays?
Also if arrays are not preferred over vectors why doesn’t everyone simply use vectors for everything?
Vectors have a dimension that may be provided at run-time, such as a user-provided value or something derived from a file’s contents.
The size of an array, by contrast, must be hard-coded into your program. It is fixed. †
There is a run-time performance cost for using vectors over arrays when you don’t need to. Personally I’ve never written code in which this is a bottleneck and therefore rarely use raw arrays; still, such scenarios do exist.
† The exception is when you create a block of memory (an “array” in some sense) dynamically with
new[]; this is the exact language feature that is encapsulated bystd::vector.