Hello again! In this video, we are going to look at reordering algorithms. These are algorithms
which take an iterator range. And they will re-arrange the order of the elements, but without
modifying their values.
And these are not sort algorithms.
We are going to have a separate video on those.
The first one we are going to look at is reverse().
This will take the elements in an iterator range and reverse their order. As you probably expect.
There is also an underscore copy version.
So this will leave the original range unmodified, and the reversed range will be written to a destination.
So let's try that out. We have a vector of ints.
We are going to make a backup of this. We could just call the copy constructor. But let's get
some practice using the copy algorithm. So we call copy with an iterator to this empty
vector.
Then we have our call to reverse().
By the way, if you ever get asked the proverbial interview question, how would you reverse a string?
This is one way to answer it. Possibly not the answer they were expecting!
What they probably expect is something that looks like this.
So you have a loop where you iterate through the elements. You swap the first element and the last element,
then the second element and the last but one.
And when you've gone past the middle, you stop.
And if you go through the various cases, you will find this works, whether the number of elements is odd or even
So that is the official solution.
But you can, even then, improve it a bit, by calling swap() instead of using these manual copies.
So anyway, back to reverse()!
So the result of calling reverse() is to reverse the order
of the elements. And we also managed to get our handwritten loop correct.
So, well done!
You have got the job!
When can you start? :)
So I will leave it to you to decide which is the better solution, that one, or that one.
There is also the rotate() algorithm. This will rotate the elements of a container around a pivot.
So let's say, for example, we have these elements and we have 3 as the pivot.
The rotation will move 3 and the elements which follow it to the front of the container. And the
elements which were before 3 will be moved to the back.
So we end up with 3 at the front, followed by 4 and 5.
And then the elements from the front, follow after those ones.
We pass
the pivot as one of the arguments, along with the iterator range.
If we want to replicate this example, we would use the third element as the pivot.
So this will be an iterator to the third element.
So we get the return value from begin() and advance it by 2.
The return value will be an iterator to the original first element, so that will be the element with
the value 1.
So here we have that code.
And then we get the return value, which is an iterator to the original first element.
So this will give the new position of that elements, the element with value 1.
And if we dereference that, we should get one.
Okay, so we have our rotate().
So 3 is the pivot. three and the following elements are at the front, and the elements before it are
at the back. And we have got an iterator to the element with value 1.
There is also a rotate_copy() version, which will leave the original unmodified and write
the result to a destination.
The return value from this is slightly different.
It is actually an iterator into the destination container.
And it will be one after the last element that was written.
So if you want to append more elements to this container, then you can use that
iterator.
So in this example, the last element that would have been written would have been the element 2.
So this will be vec2, the distinction container. And the returned iterator
will be an iterator to this element.
So that is currently the return value from end().
So we have the same code again, but we are calling rotate_copy.
For some reason, this does not seem to work with a back inserter. (Anyway!) So we get this
iterator, which will be the last plus one element of vec2.
And then, if we go back one and dereference it, we should get the last element.
Which is 2. Sorry.
OK, so the result is 3, 4, 5, 1, 2, as it was before. The last element which was written
was 2. The return value from the call will be an iterator to the element after that.
And if you go back 1 and dereference it, we get. OK, so that is it for this video.
I will see you next time.
Until then, keep coding!