Hello again! In this video, we are going to look at transform algorithms. The transform algorithm takes
an iterator range, a destination and some callable object.
It will iterate over the elements in this range and call the function with each element as argument.
The results of the function call will be stored in the destination.
So in this case, we are going to iterate over the elements of this vector, double them, and stored the
results in another vector.
So we have this data in our vector, we multiply it by 2, and we store the results in another vector.
So let's try that out.
We have our original vector with the data.
We have an empty vector, which we're going to populate and then we call transform.
We give the the iterator range for the entire vector as the source.
Our destination is going to be the empty vector and we have this lambda expression, which will multiply
each element by 2.
So we are going to populate this vector with elements which have twice the value of the elements in
this vector.
If we write this as a loop, then we just iterate over the elements, double them and push them back
on the destination vector.
So there we are.
There's the original vector, the data, and there is the new vector, which has been populated. And
the elements in this are twice the elements in the data.
Usually when algorithms write to a destination, the destination is supposed to be in a different container
or at least outside the iterator range for the data.
That is to prevent you from overwriting your source. With transform(),
you are actually allowed to overlap them. If we change the destination, so we are using begin() of the
original vector,
then this is going to overwrite all the data in the vector.
So this is going to do an in-place transformation.
So the result of this is that all the elements in the vector will be doubled.
I have just changed the destination
iterator.
So it is going to be the start of the original vector.
If we were doing this as a loop, then we would just modify the elements of the vector.
So we can see that the elements of the vector have been doubled.
There is also an overload of transform(). So this allows us to combine elements from 2 different
vectors.
So we can give it an iterator range.
So this is going to be one vector. And we can give it the start of another range, so this is going
to be the second vector.
We assume that we have the same amount of data in the two vectors. Then this is the destination
where we are going to store the result.
And this function is going to be called.
So this will iterate over the elements of this vector, and it will also fetch the corresponding element
from this vector.
And these will be passed to the function.
So the first time through, "n1" will be the first element of "vec1" and "n2" will be the first element
of "vec2".
And then we can do something with those.
In this case, we are going to add them together.
And the results of this function call will be written to "vec3".
So we have something like this. We are going to start by getting this element, and this element, and
adding them together, and storing the result as the first element of "vec3".
Then we take this element, and this element, and add them together.
And that is the next element of "vec3". And so on.
So we start off by using the same code that we had before. We take "vec1" and use that to populate
"vec2", so the elements of "vec2" are twice the elements of "vec1".
And then we use the overloaded transform() call.
So this is going to take the elements from vec1 and vec2, add them together, and store them
in vec3.
We could also write this as a loop, which just adds the elements together and calls push_back().
So here is vec1, there is vec2, and this is the vector which is obtained by adding these together.
And finally, transform() gives us yet another way to implement the equal_strings function.
So this will take two strings.
It will return [true] if they contain the same characters, but not necessarily the same case, and false if
they contain different characters.
To do this, we are going to make a copy of the argument strings, and then we are going to convert these
copies to uppercase. And we will use transform() to do that, an in-place transform.
And then finally, all the characters in the two strings are in the same case, so we can use the string's
equality operator to compare them.
So here is our function, which takes two strings.
We start off by making a copy of the two arguments, then we call transform to convert them to uppercase.
So for the left copy, we give it the iterator range.
We use the begin() iterator as the destination.
So this is going to overwrite all the data in the string. And then the function we call is the library
toupper function.
So this is a function pointer. And I put the global namespace in front of it, just to remind ourselves
it is a global function.
Then we do the same thing again for the copy of the right-hand argument. We iterate over all the elements,
we overwrite them, and we convert them to uppercase.
And then, finally, we use the string's equality operator to do the actual comparison of the characters.
The rest of the code is exactly the same as we had when we wrote the earlier implementations of
equal_string().
So let's see if this works.
OK, so if the characters are the same, but the case is different, this returns true.
If the characters are difference, then it returns false.
OK, so that is it for this video.
I will see you next time, but until then, keep coding!