5.3. Vectors

Vectors are much more similar to Python lists than arrays are. Vectors use a dynamically allocated array to store their elements, so they can change size, and they have other friendly features as well. Because they use a dynamically allocated array, they use contiguous storage locations which means that their elements can be accessed and traversed, and they can also be accessed randomly using indexes. However, vectors are dynamically sized, so their size can change automatically. A new element can be inserted into or deleted from any part of a vector, and automatic reallocation for other existing items in the vector will be applied. Unlike Python lists, vectors are homogeneous, so every element in the vector must be of the same type.

Vectors are a class that is available through a library called the Standard Template Library (STL), and one uses a < > notation to indicate the data type of the elements. In order to use vectors, One needs to include the vector library.

#include <vector>
Common C++ Vector Operators
Vector Operation Use Explanation
[ ] myvector[i] access value of element at index i
= myvector[i]=value assign value to element at index i
push_back myvect.push_back(item) Appends item to the far end of the vector
pop_back myvect.pop_back() Deletes last item (from far end) of the vector
insert myvect.insert(i, item) Inserts an item at index i
erase myvect.erase(i) Erases an element from index i
size myvect.size() Returns the actual size used by elements
capacity myvect.capacity() Returns the size of allocated storage capacity
reserve myvect.reserve(amount) Request a change in capacity to amount

A very common programming task is to grow a vector using the push_back() method to append to the vector as we see in the next example. Because vectors can change size, vectors typically allocate some extra storage to accommodate for possible growth. Thus the vector typically has an actual capacity greater than the storage size strictly needed to contain its elements.

In the above example, the use of reserve was optional. However, it is a good idea to use it before growing a vector in this way because it will save time. Because vectors are stored in underlying arrays which require contiguous memory, every time the vector’s size gets too large for the capacity, the entire vector must be moved to a larger location in memory, and all that copying takes time. In a typical implementation, the capacity is doubled each time. as in the example that follows.

Remembering that C++ is designed for speed, not protection, we will likely not be surprised by the following:

    Q-87: Which of the following is the biggest difference between a C++ array and a C++ vector?
  • Vectors can change size.
  • Right! Good job!
  • Vectors offer all of the features and protections of Python lists
  • Not all of the protections of lists are offered by vectors; one can still iterate off of either end.
  • Vectors don't use contiguous memory, so elements can be inserted.
  • No. Although elements can be inserted in vectors, they do require contiguous memory.
  • more than one of the above
  • No. Only one of the above is correct.
  • none of the above
  • One of the above is indeed correct.
    Q-88: What good is the reserve method in a vector?
  • Nothing. It is completely optional.
  • It is optional but it does serve a purpose. Try again.
  • Using it will save time if you know the maximum size needed.
  • Right!
  • It is required so memory can be allocated.
  • It is not required.
  • none of the above
  • One of the above is indeed correct.
Next Section - 5.4. Strings