Vectors-Handy, Resizable Arrays
Vectors implement a multi-purpose dynamic array. Remember that C++ arrays must be a fixed size; we cannot resize an array, and will have to create new arrays and copy the contents if we need to add another element. We have been doing this a lot in custom classes that we have created throughout this course. Vectors allow us to create a container that can be resized whenever we need it. The elements in a vector are stored contiguously—that is, laid out in a neighboring fashion—and therefore can be accessed through offsetting pointers to elements as well as iterators. We can make use of vectors by including the <vector>
header.
Vector Constructors
Vectors give us a number of different constructors to use when we want to create a new vector. A selection of these constructors are outlined here:
vector();
: Constructs an empty vector.vector(size_t n, const T& value = T());
: Constructs a vector with a number of elements...