WEBVTT

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

00:06.030 --> 00:11.400
which take an iterator range. And they will re-arrange the order of the elements, but without

00:11.400 --> 00:12.720
modifying their values.

00:13.320 --> 00:15.750
And these are not sort algorithms.

00:15.750 --> 00:17.550
We are going to have a separate video on those.

00:18.690 --> 00:20.970
The first one we are going to look at is reverse().

00:21.720 --> 00:27.660
This will take the elements in an iterator range and reverse their order. As you probably expect.

00:28.470 --> 00:30.480
There is also an underscore copy version.

00:30.870 --> 00:38.280
So this will leave the original range unmodified, and the reversed range will be written to a destination.

00:41.980 --> 00:45.430
So let's try that out. We have a vector of ints.

00:46.740 --> 00:51.150
We are going to make a backup of this. We could just call the copy constructor. But let's get

00:51.150 --> 00:56.940
some practice using the copy algorithm. So we call copy with an iterator to this empty

00:56.940 --> 00:57.360
vector.

00:58.470 --> 01:00.210
Then we have our call to reverse().

01:01.530 --> 01:06.840
By the way, if you ever get asked the proverbial interview question, how would you reverse a string?

01:07.290 --> 01:10.890
This is one way to answer it. Possibly not the answer they were expecting!

01:14.210 --> 01:16.950
What they probably expect is something that looks like this.

01:16.970 --> 01:22.340
So you have a loop where you iterate through the elements. You swap the first element and the last element,

01:22.340 --> 01:24.170
then the second element and the last but one.

01:24.800 --> 01:26.930
And when you've gone past the middle, you stop.

01:27.590 --> 01:32.930
And if you go through the various cases, you will find this works, whether the number of elements is odd or even

01:34.640 --> 01:36.410
So that is the official solution.

01:36.740 --> 01:42.680
But you can, even then, improve it a bit, by calling swap() instead of using these manual copies.

01:44.910 --> 01:46.590
So anyway, back to reverse()!

01:51.380 --> 01:54.770
So the result of calling reverse() is to reverse the order

01:54.840 --> 01:58.250
of the elements. And we also managed to get our handwritten loop correct.

01:59.480 --> 02:00.340
So, well done!

02:00.410 --> 02:01.040
You have got the job!

02:01.670 --> 02:02.450
When can you start? :)

02:05.130 --> 02:11.160
So I will leave it to you to decide which is the better solution, that one, or that one.

02:14.470 --> 02:21.790
There is also the rotate() algorithm. This will rotate the elements of a container around a pivot.

02:23.050 --> 02:27.550
So let's say, for example, we have these elements and we have 3 as the pivot.

02:28.570 --> 02:34.750
The rotation will move 3 and the elements which follow it to the front of the container. And the

02:34.750 --> 02:38.290
elements which were before 3 will be moved to the back.

02:38.890 --> 02:42.100
So we end up with 3 at the front, followed by 4 and 5.

02:42.520 --> 02:46.660
And then the elements from the front, follow after those ones.

02:49.660 --> 02:50.260
We pass

02:50.270 --> 02:53.230
the pivot as one of the arguments, along with the iterator range.

02:54.130 --> 02:58.780
If we want to replicate this example, we would use the third element as the pivot.

02:59.170 --> 03:01.570
So this will be an iterator to the third element.

03:02.230 --> 03:05.590
So we get the return value from begin() and advance it by 2.

03:06.930 --> 03:12.690
The return value will be an iterator to the original first element, so that will be the element with

03:12.690 --> 03:13.560
the value 1.

03:19.710 --> 03:21.590
So here we have that code.

03:25.120 --> 03:30.220
And then we get the return value, which is an iterator to the original first element.

03:30.610 --> 03:34.360
So this will give the new position of that elements, the element with value 1.

03:34.750 --> 03:36.550
And if we dereference that, we should get one.

03:39.090 --> 03:40.700
Okay, so we have our rotate().

03:40.740 --> 03:46.540
So 3 is the pivot. three and the following elements are at the front, and the elements before it are

03:46.590 --> 03:50.310
at the back. And we have got an iterator to the element with value 1.

03:55.060 --> 04:01.440
There is also a rotate_copy() version, which will leave the original unmodified and write

04:01.450 --> 04:03.010
the result to a destination.

04:04.600 --> 04:06.910
The return value from this is slightly different.

04:07.630 --> 04:11.590
It is actually an iterator into the destination container.

04:12.070 --> 04:15.070
And it will be one after the last element that was written.

04:15.610 --> 04:19.460
So if you want to append more elements to this container, then you can use that

04:19.480 --> 04:20.140
iterator.

04:21.990 --> 04:26.260
So in this example, the last element that would have been written would have been the element 2.

04:27.150 --> 04:31.470
So this will be vec2, the distinction container. And the returned iterator

04:31.470 --> 04:33.570
will be an iterator to this element.

04:34.410 --> 04:36.780
So that is currently the return value from end().

04:42.060 --> 04:45.120
So we have the same code again, but we are calling rotate_copy.

04:47.350 --> 04:53.010
For some reason, this does not seem to work with a back inserter. (Anyway!) So we get this

04:53.020 --> 04:57.100
iterator, which will be the last plus one element of vec2.

04:57.850 --> 05:01.890
And then, if we go back one and dereference it, we should get the last element.

05:03.930 --> 05:05.140
Which is 2. Sorry.

05:14.470 --> 05:20.530
OK, so the result is 3, 4, 5, 1, 2, as it was before. The last element which was written

05:20.530 --> 05:25.630
was 2. The return value from the call will be an iterator to the element after that.

05:26.620 --> 05:31.480
And if you go back 1 and dereference it, we get. OK, so that is it for this video.

05:31.870 --> 05:32.740
I will see you next time.

05:32.950 --> 05:34.570
Until then, keep coding!
