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.
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.
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.
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}
.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.
.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}
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}
.clear() method of the vector:
// remove all the elements:
values.erase(values.begin(), values.end());
// or more succinctly:
values.clear();
.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:
.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.
.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());
vec1.push_back().
nums contain?
vector<int> nums = {10, 12, 15, 18, 24};
nums.erase(nums.begin() + 2);
10 12 18 2410 15 18 2410 1510 12nums contain?
vector<int> nums = {10, 12, 15, 18, 24};
nums.insert(nums.begin() + 1, 8);
10 8 12 15 18 248 10 12 15 18 248 12 15 18 2410 8 15 18 24nums contain?
vector<int> nums = {10, 12, 15, 18, 24};
nums.erase(nums.begin() + 1, nums.begin() + 3);
10 18 2410 2418 2415 18 24