Skip to main content

Section 13.6 Erasing and Inserting Elements

If we do want to remove an element at a location other than the end, there is an erase member function. You might expect that it takes an index as its parameter, like vec.erase(2). Instead, it requires an iterator be passed as the parameter.
What are iterators? Iterators are objects that store (and manipulate) a location in a collection of data. For now, all we need to know about iterators is that they represent a position in a vector.
To make an iterator that points to index X of a vector named vectorName, you can specify vectorName.begin() + X. .begin() makes an iterator that points to the first element of the vector. If you want to point at index 0, you can just say vectorName.begin(). When you add + X, it moves the iterator forward by X positions.
For example:
vector<int> values = {1, 2, 3, 4, 5};

values.erase(values.begin() + 2); // remove the element at index 2
                                  // now values is {1, 2, 4, 5}

// Starting from {1, 2, 4, 5}
values.erase(values.begin());     // remove the element at index 0
                                  // now values is {2, 4, 5}
It is also possible to specify is also a .end() function that makes an iterator that points to one location PAST the end of the vector. (This sounds odd, but ends up being useful as an indicator that we have gone too far and moved past the last element.) Starting from the .end() we can do - X to move backwards by X positions. This means that the last element can be pointed at using .end() - 1.
Figure 13.6.1. A vector and some iterators. Note that .end() points one past the last element. The last element is at .end() - 1.
vector<int> values = {1, 2, 3, 4, 5};

values.erase(values.end() - 1);   // remove the last element
                                  // now values is {1, 2, 3, 4}

// Starting from {1, 2, 3, 4}
values.erase(values.end() - 2);   // remove the next to last element
                                  // now values is {1, 2, 4}
It is possible to remove a whole range of elements by specifying a start and end location. All the items from start, up to but not including the end location, will be removed. This means that to specify removing up to and including the last element, we want to pass in the location that is AFTER the last element. In a vector with 3 items, the last element is at index 2. So we would use vectorName.begin() + 3 to specify β€œuntil the end of the vector”. Fortunately, rather than do this, we can also just say vectorName.end():
vector<int> values = {1, 2, 3, 4, 5, 6, 7, 8};
// remove all the elements from index 0 up to (but not including) index 3
values.erase(values.begin(), values.begin() + 3);
// now values is {4, 5, 6, 7, 8}
// remove all the elements from index 2 to the end
values.erase(values.begin() + 2, values.end());
// now values is {4, 5}
To clear out all the elements in a vector, we can either say to erase from the beginning to the end, or use the .clear() method of the vector:
// remove all the elements:
values.erase(values.begin(), values.end());
// or more succinctly:
values.clear();
The .insert(iterator, value) function works by the same logic to specify where you wish to insert a value. Whatever value is currently at that location and anything after it are β€œpushed over” one location so that the new value can be placed in the specified location. This program uses insert to add some items to a vector:
Listing 13.6.2.
Specifying an invalid location for either .insert() or .erase() will result in the program crashing. There will not be an exception thrown! You can test this out in the sample above by specifying something like letters.begin() + 100 for one of the locations.

Warning 13.6.1.

Be very careful with iterators! If your program crashes with no output, or behaves unexpectedly, it might be due to invalid iterator locations.

Note 13.6.2.

We aren’t covering every possible variation of .insert(). For example, if you want to add all the elements from vec2 to the end of vec1, you can use the insert function with a range of iterators:
vec1.insert(vec1.end(), vec2.begin(), vec2.end());
To say β€œinsert at the end of vec1 all the items from the beginning of vec2 to the end of vec2.”. Of course, you could also loop through vec2 and insert each element individually using vec1.push_back().
While the insert version is more concise, it isn’t essential to know. When you want to do something with a vector, think in terms of the basic tools first before looking for a fancy shortcut.

Checkpoint 13.6.1.

Given this sequence of operations, what would nums contain?
vector<int> nums = {10, 12, 15, 18, 24};
nums.erase(nums.begin() + 2);
  • 10 12 18 24
  • 10 15 18 24
  • 10 15
  • 10 12

Checkpoint 13.6.2.

Given this sequence of operations, what would nums contain?
vector<int> nums = {10, 12, 15, 18, 24};
nums.insert(nums.begin() + 1, 8);
  • 10 8 12 15 18 24
  • 8 10 12 15 18 24
  • 8 12 15 18 24
  • 10 8 15 18 24

Checkpoint 13.6.3.

Given this sequence of operations, what would nums contain?
vector<int> nums = {10, 12, 15, 18, 24};
nums.erase(nums.begin() + 1, nums.begin() + 3);
  • 10 18 24
  • 10 24
  • 18 24
  • 15 18 24
You have attempted of activities on this page.