WEBVTT

00:00.180 --> 00:07.200
Hello again! In this video, we are going to look at transform algorithms. The transform algorithm takes

00:07.200 --> 00:11.010
an iterator range, a destination and some callable object.

00:11.790 --> 00:17.910
It will iterate over the elements in this range and call the function with each element as argument.

00:18.390 --> 00:21.720
The results of the function call will be stored in the destination.

00:23.630 --> 00:29.540
So in this case, we are going to iterate over the elements of this vector, double them, and stored the

00:29.540 --> 00:31.130
results in another vector.

00:34.700 --> 00:41.000
So we have this data in our vector, we multiply it by 2, and we store the results in another vector.

00:43.650 --> 00:44.940
So let's try that out.

00:45.270 --> 00:47.910
We have our original vector with the data.

00:48.300 --> 00:52.320
We have an empty vector, which we're going to populate and then we call transform.

00:52.980 --> 00:57.180
We give the the iterator range for the entire vector as the source.

00:57.870 --> 01:03.180
Our destination is going to be the empty vector and we have this lambda expression, which will multiply

01:03.180 --> 01:04.290
each element by 2.

01:05.460 --> 01:10.110
So we are going to populate this vector with elements which have twice the value of the elements in

01:10.110 --> 01:10.770
this vector.

01:12.330 --> 01:17.790
If we write this as a loop, then we just iterate over the elements, double them and push them back

01:17.790 --> 01:19.200
on the destination vector.

01:22.220 --> 01:23.030
So there we are.

01:23.060 --> 01:28.880
There's the original vector, the data, and there is the new vector, which has been populated. And

01:28.880 --> 01:32.210
the elements in this are twice the elements in the data.

01:37.380 --> 01:43.320
Usually when algorithms write to a destination, the destination is supposed to be in a different container

01:43.590 --> 01:46.770
or at least outside the iterator range for the data.

01:47.190 --> 01:51.720
That is to prevent you from overwriting your source. With transform(),

01:51.930 --> 01:58.770
you are actually allowed to overlap them. If we change the destination, so we are using begin() of the

01:58.770 --> 01:59.610
original vector,

02:00.060 --> 02:02.580
then this is going to overwrite all the data in the vector.

02:03.090 --> 02:05.550
So this is going to do an in-place transformation.

02:06.060 --> 02:09.510
So the result of this is that all the elements in the vector will be doubled.

02:12.700 --> 02:14.500
I have just changed the destination

02:14.530 --> 02:15.090
iterator.

02:15.460 --> 02:19.150
So it is going to be the start of the original vector.

02:20.830 --> 02:25.360
If we were doing this as a loop, then we would just modify the elements of the vector.

02:29.020 --> 02:32.140
So we can see that the elements of the vector have been doubled.

02:35.570 --> 02:41.030
There is also an overload of transform(). So this allows us to combine elements from 2 different

02:41.030 --> 02:41.570
vectors.

02:44.040 --> 02:46.000
So we can give it an iterator range.

02:46.050 --> 02:51.390
So this is going to be one vector. And we can give it the start of another range, so this is going

02:51.390 --> 02:52.590
to be the second vector.

02:53.670 --> 02:58.860
We assume that we have the same amount of data in the two vectors. Then this is the destination

02:58.860 --> 03:00.120
where we are going to store the result.

03:00.900 --> 03:03.330
And this function is going to be called.

03:03.540 --> 03:09.150
So this will iterate over the elements of this vector, and it will also fetch the corresponding element

03:09.150 --> 03:09.930
from this vector.

03:10.530 --> 03:12.330
And these will be passed to the function.

03:12.720 --> 03:18.450
So the first time through, "n1" will be the first element of "vec1" and "n2" will be the first element

03:18.450 --> 03:19.450
of "vec2".

03:20.280 --> 03:21.630
And then we can do something with those.

03:22.020 --> 03:23.670
In this case, we are going to add them together.

03:24.240 --> 03:27.690
And the results of this function call will be written to "vec3".

03:29.150 --> 03:34.430
So we have something like this. We are going to start by getting this element, and this element, and

03:34.430 --> 03:38.900
adding them together, and storing the result as the first element of "vec3".

03:39.470 --> 03:42.350
Then we take this element, and this element, and add them together.

03:42.710 --> 03:45.170
And that is the next element of "vec3". And so on.

03:47.130 --> 03:52.380
So we start off by using the same code that we had before. We take "vec1" and use that to populate

03:52.380 --> 03:56.310
"vec2", so the elements of "vec2" are twice the elements of "vec1".

03:57.450 --> 04:00.210
And then we use the overloaded transform() call.

04:00.900 --> 04:06.090
So this is going to take the elements from vec1 and vec2, add them together, and store them

04:06.100 --> 04:06.840
in vec3.

04:08.590 --> 04:12.850
We could also write this as a loop, which just adds the elements together and calls push_back().

04:15.960 --> 04:22.830
So here is vec1, there is vec2, and this is the vector which is obtained by adding these together.

04:25.820 --> 04:30.320
And finally, transform() gives us yet another way to implement the equal_strings function.

04:30.770 --> 04:32.900
So this will take two strings.

04:33.410 --> 04:38.900
It will return [true] if they contain the same characters, but not necessarily the same case, and false if

04:38.900 --> 04:40.130
they contain different characters.

04:41.960 --> 04:47.210
To do this, we are going to make a copy of the argument strings, and then we are going to convert these

04:47.210 --> 04:52.190
copies to uppercase. And we will use transform() to do that, an in-place transform.

04:52.850 --> 04:57.740
And then finally, all the characters in the two strings are in the same case, so we can use the string's

04:57.750 --> 04:59.360
equality operator to compare them.

05:02.230 --> 05:05.920
So here is our function, which takes two strings.

05:06.370 --> 05:12.520
We start off by making a copy of the two arguments, then we call transform to convert them to uppercase.

05:13.390 --> 05:17.260
So for the left copy, we give it the iterator range.

05:18.760 --> 05:21.460
We use the begin() iterator as the destination.

05:21.730 --> 05:28.030
So this is going to overwrite all the data in the string. And then the function we call is the library

05:28.030 --> 05:28.900
toupper function.

05:29.170 --> 05:36.190
So this is a function pointer. And I put the global namespace in front of it, just to remind ourselves

05:36.190 --> 05:38.050
it is a global function.

05:38.590 --> 05:43.240
Then we do the same thing again for the copy of the right-hand argument. We iterate over all the elements,

05:43.240 --> 05:46.240
we overwrite them, and we convert them to uppercase.

05:48.080 --> 05:53.390
And then, finally, we use the string's equality operator to do the actual comparison of the characters.

05:54.800 --> 05:59.930
The rest of the code is exactly the same as we had when we wrote the earlier implementations of

05:59.930 --> 06:00.320
equal_string().

06:00.710 --> 06:01.910
So let's see if this works.

06:05.030 --> 06:09.740
OK, so if the characters are the same, but the case is different, this returns true.

06:10.700 --> 06:13.190
If the characters are difference, then it returns false.

06:14.150 --> 06:16.010
OK, so that is it for this video.

06:16.400 --> 06:19.670
I will see you next time, but until then, keep coding!
