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.
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.
Subsection 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.
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:
Reading Questions Reading Questions
1.
2.
3.
Which of the following is the biggest difference between a C++ array and a C++ vector?
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.
One of the above is indeed correct.
4.
What good is the
reserve
method in a vector?