Hello again! In this video, we are going to look at removing algorithms.
You can probably guess what these do, but they may not do it in the way that you expect.
So these will remove elements from an iterator range, but they will only "logically" remove them.
They will not actually destroy the elements.
So the removed elements are still physically present in the container.
The remove algorithm call takes the iterator range and some object.
Each element which is equal to that object, is moved to the back of the container, so the removed
element still exists.
Its value is no longer defined.
So accessing it, or trying to access, it is undefined behavior.
The other elements which are behind it are moved forwards, to make room for it.
So the container still has the same number of elements.
The return value from remove is an iterator to the first removed element.
So let's imagine we have a vector with the elements 3, 1, 4, 1, 5, 9.
So it looks like this. We have the control block with the element count, six. And a pointer to the
data, and the data has 3, 1, 4, 1, 5, 9.
If we call remove() and we give the entire vector as the iterator range and the argument 1,
this will remove the elements with value 1.
So these two elements are going to be moved to the back of the container and all the elements behind
them are going to be moved forwards.
So the vector is going to look like this.
We have the elements which are not equal to 1 at the front of the vector, and the elements which are
equal to 1 are at the back of the vector.
So these are the removed elements. And their value is no longer defined.
The element count is still 6.
The vector still thinks it has 6 elements. The return value
from remove() will be an iterator to this one, the first removed element.
So let's try this out.
We have our vector with those elements. Then we print out the data and size.
Then we call remove().
We give the entire container as the estimated range, and 1 as the value to be removed.
And then we print out the result.
So let's see what happens.
So there's the original vector, with 6 elements.
After calling remove(), the vector still has 6 elements and we can see that
all the elements, which are not 1, have been moved forwards.
And then these two actually have the same value.
So it looks as though this implementation has done a slight optimization, by not actually overwriting
these elements.
So these elements could be five and nine.
They could be one one, or they could be complete garbage.
Who knows! If we want to actually, physically, remove the elements from the container,
we need to call the erase() member function for that container.
erase() will take an iterator, which is the first element to be erased and a "last plus one" iterator,
which is one after the last element to be erased. When we call remove(), we get an iterator
returned, which is an iterator to the first removed element.
So if we pass that as the first argument to erase(), it will start erasing from this element, and it will
continue until that one.
So this will erase all the remaining elements in the vector.
And then we have a vector, which contains 3, 4, 5 and 9.
So this will physically remove the elements from the container.
So the vector will now look like this. We just have the elements which are not equal to 1:
3, 4, 5 and 9.
The elements which were equal to 1 have been destroyed.
They are no longer in the container, and the vector now has 4 elements instead of 6.
So here is the code we had before. We call remove() and we get the returned iterator.
Then we pass that iterator as the first argument to erase(), and then we print out the vector again.
So let's see what we get this time.
So there is the original vector with 6 elements.
There is the vector after calling remove(). And then we call erase().
So the vector now has 4 elements, which are 3, 4, 5 and 9.
So the elements with value 1 have disappeared. They have actually been destroyed and are no longer part of the container.
OK, so that is it for this video.
I will see you next time, but until then, keep coding!