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>
Vector Operation |
Use |
Explanation |
---|---|---|
|
|
access value of element at index i |
|
|
assign value to element at index i |
|
|
Appends item to the far end of the vector |
|
|
Deletes last item (from far end) of the vector |
|
|
Inserts an item at index i |
|
|
Erases an element from index i |
|
|
Returns the actual size used by elements |
|
|
Returns the size of allocated storage capacity |
|
|
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¶
-
Q-2: Match the Vector operations with their corresponding explination.
Feedback shows incorrect matches.
- [ ]
- Accesses value of an element.
- =
- Assigns value to an element.
- push_back
- Appends item to the end of the vector.
- pop_back
- Deletes last item of the vector.
- insert
- Injects an item into the vector.
- erase
- Deletes an element from the choosen index.
- size
- Returns the actual capacity used by elements.
- capacity
- Returns the ammount of allocated storage space.
- reserve
- Request a change in space to amount
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:
- 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-8: Which of the following is the biggest difference between a C++ array and a C++ 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.
Q-9: What good is the reserve
method in a vector?