WEBVTT

00:00.120 --> 00:06.150
Hello again! In this video, we are going to look at write algorithms. A write algorithm is one which can

00:06.150 --> 00:08.430
change the elements in an iterator range.

00:09.300 --> 00:11.460
The first one we are going to look at is replace().

00:12.090 --> 00:14.910
This will take two arguments, as well as the iterator range.

00:15.690 --> 00:20.910
If there is any element which has the first value, then it will be replaced by an element with the

00:20.910 --> 00:21.570
second value.

00:22.740 --> 00:29.640
For example, if we have a vector and we pass begin() and end(), and 1 and 2, then any elements in this

00:29.640 --> 00:35.700
vector which have the value 1 will have the value 2. If we write this as a loop,

00:35.910 --> 00:41.610
then if the value is 1, set it to 2, otherwise leave it unchanged.

00:44.270 --> 00:46.350
So here is some code to try that out.

00:47.030 --> 00:48.050
We have our vector.

00:50.550 --> 00:56.130
We have the replace() call, so replace elements with value 1 by elements with value 2.

00:57.300 --> 00:58.650
Then we have the handwritten loop.

01:01.700 --> 01:08.700
If the element value is 1, make it 2. Otherwise do nothing. And there we are: the elements with

01:08.700 --> 01:10.920
value 1, now have value 2.

01:12.680 --> 01:15.290
There is also a version which takes a predicate.

01:15.770 --> 01:22.070
If the predicate returns true, then set the value of the element to the fourth argument. So we

01:22.070 --> 01:26.210
can call replace_if() with the iterator range, and then a callable object.

01:26.750 --> 01:33.350
In this case, a lambda expression. And for a change, we are going to work on even numbers: if they are

01:33.380 --> 01:34.820
exactly divisible by 2,

01:35.120 --> 01:36.770
then set the value to 6.

01:38.560 --> 01:44.800
And we could also write a loop in which, if the number is divisible by 2, then change its value.

01:48.960 --> 01:54.030
So there is the call to replace_if() with the lambda expression and the new value of 6.

01:57.750 --> 02:00.690
So there we are. There was 1 even element, which was 4.

02:01.050 --> 02:02.580
And now it is 6.

02:03.180 --> 02:09.330
When an algorithm modifies the data in an iterator range, the library usually provides two versions.

02:10.170 --> 02:13.170
There is a base version which will make the changes in-place.

02:13.500 --> 02:14.640
So this will overwrite

02:14.640 --> 02:16.870
the data in the original iterator range.

02:17.640 --> 02:23.160
And there is another version with underscore copy in the name, and this will write the modified data

02:23.520 --> 02:24.840
to a different iterator range.

02:25.290 --> 02:27.720
So the original data is unchanged.

02:29.390 --> 02:33.800
So this is useful for working with data which is important, or you want to use later.

02:34.550 --> 02:39.110
And also, it makes life easier when you are working with associative containers. As you will see

02:39.110 --> 02:44.120
when we get onto those, modifying the elements of an associative container can be a little bit more

02:44.120 --> 02:44.600
involved.

02:45.470 --> 02:50.330
The replace() algorithm has a _copy version, which will write the data to some destination.

02:51.680 --> 02:55.610
So we need to give an iterator, to tell the algorithm where to write this data.

02:56.000 --> 02:58.040
And usually, that will be an insert iterator.

02:59.120 --> 03:04.520
So we have the iterator range that we are processing. So we can use const iterators here, because

03:04.520 --> 03:05.810
this is not going to be modified.

03:06.500 --> 03:09.350
Then we have the iterator for where we are writing the data.

03:10.130 --> 03:11.090
Then we have the value

03:11.090 --> 03:13.790
we want to modify. And then we have the new value.

03:14.540 --> 03:20.780
So this is going to leave the original vector unchanged and write out a new vector with the same elements.

03:20.900 --> 03:22.940
Except that elements with the value 1,

03:22.970 --> 03:23.930
now have the value 2.

03:28.760 --> 03:31.220
So here is our call to replace_copy().

03:32.660 --> 03:36.770
We are going to iterate through the elements of this vector and copy them to a new vector.

03:37.400 --> 03:42.800
If the element value is 1, then the new vector will have 2. Otherwise, it will have the same value.

03:45.050 --> 03:50.630
If we were writing this by hand, we would push back a different value, depending on the value of the element.

03:50.960 --> 03:53.720
If the element is 1, then we would push back 2.

03:54.170 --> 03:56.960
Otherwise, we just push back the original value.

04:00.600 --> 04:01.260
And there we are.

04:01.470 --> 04:08.010
So we have a new vector in which 3, 4, 5, 9 are the same, but 1 is replaced by 2.

04:10.100 --> 04:16.250
And there is also a copy version of replace_if(). It is called replace_copy_if(). So this will take

04:16.250 --> 04:18.260
the iterator range from the original vector.

04:18.680 --> 04:21.860
The destination iterator. The lambda expression.

04:22.580 --> 04:27.350
And if this lambda expression returns true, the element in the new vector will have the value

04:27.370 --> 04:30.800
six. Otherwise, it have the same value that it does in "vec1".

04:34.820 --> 04:36.800
So there it is, replace_copy_if().

04:38.260 --> 04:44.470
The lambda expression: if the number is even, it will have the value 6, otherwise it will have the original

04:44.470 --> 04:44.800
value.

04:47.460 --> 04:51.870
And again, we could write a loop with if statements and push_backs.

04:54.630 --> 05:00.260
So there we are, we have the even value 4 in the original vector and in the new vector, that is

05:00.270 --> 05:00.720
six.

05:00.990 --> 05:02.760
But the other elements all have the same values.

05:04.230 --> 05:05.820
OK, so that is it for this video.

05:06.300 --> 05:07.140
I will see you next time.

05:07.140 --> 05:09.240
But until then, keep coding!
