The container class template vector generalizes the concept of an ordinary C array. Like an array, a vector is an indexed data structure, with index values that range from 0 to one less than the number of elements contained in the structure. Elements in a vector can be accessed with a subscript, like an array. However, a vector differs from an array in the following important respects:
The size of a vector changes dynamically. New elements can be inserted on to the end of a vector, or into the middle. Storage management is handled automatically. Insertion at the end of a vector is efficient. However, insertion into the middle of a vector is not as efficient as insertion into the middle of a list (Section 6). If many insertion operations into the middle of the container are to be performed, a list container should be used.
A vector has more self-knowledge than an ordinary array. In particular, a vector can be queried about the number of elements it contains, the number of elements which it can contain without allocating more memory, the maximum number of elements it can potentially contain, and so on.
The container class template vector in the C++ Standard Library should be compared and contrasted to the container class template deque described in Chapter 7. Like a vector, a deque (pronounced deck) is an indexed data structure. The major difference between the two is that a deque provides efficient insertion at either the beginning or the end of the container, while a vector provides efficient insertion only at the end. In many situations, either structure can be used. Use of a vector generally results in a smaller executable file, while use of a deque may result in a slightly faster program, depending upon the particular set of operations being performed.
Whenever you use a vector, you must include the vector header file:
#include <vector>