WEBVTT

00:00.180 --> 00:04.020
Hello again! In this video, we are going to look at removing algorithms.

00:05.450 --> 00:10.010
You can probably guess what these do, but they may not do it in the way that you expect.

00:10.370 --> 00:15.770
So these will remove elements from an iterator range, but they will only "logically" remove them.

00:16.070 --> 00:17.780
They will not actually destroy the elements.

00:18.320 --> 00:22.310
So the removed elements are still physically present in the container.

00:24.420 --> 00:28.740
The remove algorithm call takes the iterator range and some object.

00:29.880 --> 00:35.250
Each element which is equal to that object, is moved to the back of the container, so the removed

00:35.250 --> 00:36.570
element still exists.

00:37.080 --> 00:38.850
Its value is no longer defined.

00:39.360 --> 00:43.050
So accessing it, or trying to access, it is undefined behavior.

00:44.550 --> 00:48.330
The other elements which are behind it are moved forwards, to make room for it.

00:48.690 --> 00:51.150
So the container still has the same number of elements.

00:54.240 --> 00:59.910
The return value from remove is an iterator to the first removed element.

01:01.930 --> 01:05.680
So let's imagine we have a vector with the elements 3, 1, 4, 1, 5, 9.

01:06.160 --> 01:11.620
So it looks like this. We have the control block with the element count, six. And a pointer to the

01:11.620 --> 01:15.100
data, and the data has 3, 1, 4, 1, 5, 9.

01:16.120 --> 01:21.700
If we call remove() and we give the entire vector as the iterator range and the argument 1,

01:22.210 --> 01:24.640
this will remove the elements with value 1.

01:25.360 --> 01:30.580
So these two elements are going to be moved to the back of the container and all the elements behind

01:30.580 --> 01:32.260
them are going to be moved forwards.

01:34.220 --> 01:35.990
So the vector is going to look like this.

01:36.230 --> 01:41.180
We have the elements which are not equal to 1 at the front of the vector, and the elements which are

01:41.180 --> 01:43.580
equal to 1 are at the back of the vector.

01:43.850 --> 01:47.900
So these are the removed elements. And their value is no longer defined.

01:49.280 --> 01:51.110
The element count is still 6.

01:51.110 --> 01:55.790
The vector still thinks it has 6 elements. The return value

01:55.790 --> 02:00.230
from remove() will be an iterator to this one, the first removed element.

02:01.580 --> 02:02.810
So let's try this out.

02:03.290 --> 02:08.630
We have our vector with those elements. Then we print out the data and size.

02:09.080 --> 02:10.160
Then we call remove().

02:10.820 --> 02:16.010
We give the entire container as the estimated range, and 1 as the value to be removed.

02:16.850 --> 02:18.320
And then we print out the result.

02:19.070 --> 02:20.120
So let's see what happens.

02:22.100 --> 02:24.710
So there's the original vector, with 6 elements.

02:25.340 --> 02:29.510
After calling remove(), the vector still has 6 elements and we can see that

02:29.510 --> 02:32.660
all the elements, which are not 1, have been moved forwards.

02:33.140 --> 02:36.050
And then these two actually have the same value.

02:36.380 --> 02:41.090
So it looks as though this implementation has done a slight optimization, by not actually overwriting

02:41.090 --> 02:41.690
these elements.

02:43.550 --> 02:45.640
So these elements could be five and nine.

02:45.680 --> 02:48.620
They could be one one, or they could be complete garbage.

02:49.070 --> 02:53.660
Who knows! If we want to actually, physically, remove the elements from the container,

02:54.050 --> 02:57.140
we need to call the erase() member function for that container.

02:59.230 --> 03:06.780
erase() will take an iterator, which is the first element to be erased and a "last plus one" iterator,

03:06.900 --> 03:13.230
which is one after the last element to be erased. When we call remove(), we get an iterator

03:13.230 --> 03:16.470
returned, which is an iterator to the first removed element.

03:16.920 --> 03:23.400
So if we pass that as the first argument to erase(), it will start erasing from this element, and it will

03:23.400 --> 03:24.690
continue until that one.

03:24.900 --> 03:28.530
So this will erase all the remaining elements in the vector.

03:29.850 --> 03:33.000
And then we have a vector, which contains 3, 4, 5 and 9.

03:33.690 --> 03:36.300
So this will physically remove the elements from the container.

03:38.210 --> 03:42.350
So the vector will now look like this. We just have the elements which are not equal to 1:

03:42.830 --> 03:44.000
3, 4, 5 and 9.

03:44.780 --> 03:48.230
The elements which were equal to 1 have been destroyed.

03:48.240 --> 03:53.120
They are no longer in the container, and the vector now has 4 elements instead of 6.

03:55.030 --> 03:59.500
So here is the code we had before. We call remove() and we get the returned iterator.

04:01.180 --> 04:08.200
Then we pass that iterator as the first argument to erase(), and then we print out the vector again.

04:08.710 --> 04:10.480
So let's see what we get this time.

04:13.000 --> 04:15.250
So there is the original vector with 6 elements.

04:15.310 --> 04:19.270
There is the vector after calling remove(). And then we call erase().

04:19.810 --> 04:23.170
So the vector now has 4 elements, which are 3, 4, 5 and 9.

04:23.590 --> 04:29.740
So the elements with value 1 have disappeared. They have actually been destroyed and are no longer part of the container.

04:30.520 --> 04:32.080
OK, so that is it for this video.

04:32.380 --> 04:35.440
I will see you next time, but until then, keep coding!
