A vector is a dynamic array with automatically handled storage. The elements in a vector can be accessed just as efficiently as those in an array with the advantage being that vectors can dynamically change in size.
In terms of storage the vector data is (usually) placed in dynamically allocated memory thus requiring some minor overhead; conversely C-arrays
and std::array
use automatic storage relative to the declared location and thus do not have any overhead.
Use of an std::vector
requires the inclusion of the <vector>
header using #include <vector>
.
Elements in a std::vector
are stored contiguously on the free store. It should be noted that when vectors are nested as in std::vector<std::vector<int> >
, the elements of each vector are contiguous, but each vector allocates its own underlying buffer on the free store.