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.

5.3.1. Iterating through Vectors

When iterating vectors, you must first find the length of your container. You can simply call the .length() function. For arrays, the number of elements can be found by getting the size in memory of the array by using the sizeof() function, and then dividing it by the size of the first element of the array using the same sizeof() function. Because all elements in C++ arrays are the same type, they take the same amount of space and that can be used to find the number of elements the Array contains!

An optional secondary version of the for loop has been commented out of the above code. You can try running this in your version of C++ to see if it works, but in some older versions of C++, such as C++98, it does not.

The above loop assigns the variable index to be each successive value from 0 to numsSize.

Then, the value at that index in the array is printed to the console.

5.3.1.1. Matching

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:

You have attempted of activities on this page