I'm fairly new to C++ and STL, so my interpretation may be perhaps flawed. According to The C++ Standard Library: A Tutorial and Reference;
capacity() returns the number of elements a vector could contain in its actual memory. If you exceed the capacity(), the vector has to reallocate its internal memory.
The capacity of a vector is important for two reasons:
1. Reallocation invalidates all references, pointers, and iterators for elements of the vector.
2. Reallocation takes time.
Thus, if a program managed pointers, references, or iterators into a vector, or if speed is a goal, it is important to take the capacity into account. (149)
...
Regarding performance, you should consider that inserting and removing happens faster when
- Elements are inserted or removed at the end
- The capacity is large enough on entry
- Multiple elements are inserted by a single call rather than by multiple calls
Inserting or removing elements invalidates references, pointers, and iterators that refer to the following elements. If an insertion causes reallocation, it invalidates all references, iterators, and pointers. (153)
On page 166-167, the book describes the differences between Lists and Vectors. For Lists, insertion and removal is much faster at any position, because it does not invalidate pointers or references. However, Lists do not provide random access, and accessing specific elements in a List is slow. On the other hand, random access in a Vector is quite fast.
Obviously you can create a Vector with a large capacity to begin with, but in the case of the original post, I seriously doubt he'll know how much capacity would be required. Also, it sounds like he won't make use of random access, either.
So yes, I was wrong when I stated that "an array that rebuilds itself every time you add an item." However, I wasn't too far from the truth.