Hello again! In this video, we are going to look at write algorithms. A write algorithm is one which can
change the elements in an iterator range.
The first one we are going to look at is replace().
This will take two arguments, as well as the iterator range.
If there is any element which has the first value, then it will be replaced by an element with the
second value.
For example, if we have a vector and we pass begin() and end(), and 1 and 2, then any elements in this
vector which have the value 1 will have the value 2. If we write this as a loop,
then if the value is 1, set it to 2, otherwise leave it unchanged.
So here is some code to try that out.
We have our vector.
We have the replace() call, so replace elements with value 1 by elements with value 2.
Then we have the handwritten loop.
If the element value is 1, make it 2. Otherwise do nothing. And there we are: the elements with
value 1, now have value 2.
There is also a version which takes a predicate.
If the predicate returns true, then set the value of the element to the fourth argument. So we
can call replace_if() with the iterator range, and then a callable object.
In this case, a lambda expression. And for a change, we are going to work on even numbers: if they are
exactly divisible by 2,
then set the value to 6.
And we could also write a loop in which, if the number is divisible by 2, then change its value.
So there is the call to replace_if() with the lambda expression and the new value of 6.
So there we are. There was 1 even element, which was 4.
And now it is 6.
When an algorithm modifies the data in an iterator range, the library usually provides two versions.
There is a base version which will make the changes in-place.
So this will overwrite
the data in the original iterator range.
And there is another version with underscore copy in the name, and this will write the modified data
to a different iterator range.
So the original data is unchanged.
So this is useful for working with data which is important, or you want to use later.
And also, it makes life easier when you are working with associative containers. As you will see
when we get onto those, modifying the elements of an associative container can be a little bit more
involved.
The replace() algorithm has a _copy version, which will write the data to some destination.
So we need to give an iterator, to tell the algorithm where to write this data.
And usually, that will be an insert iterator.
So we have the iterator range that we are processing. So we can use const iterators here, because
this is not going to be modified.
Then we have the iterator for where we are writing the data.
Then we have the value
we want to modify. And then we have the new value.
So this is going to leave the original vector unchanged and write out a new vector with the same elements.
Except that elements with the value 1,
now have the value 2.
So here is our call to replace_copy().
We are going to iterate through the elements of this vector and copy them to a new vector.
If the element value is 1, then the new vector will have 2. Otherwise, it will have the same value.
If we were writing this by hand, we would push back a different value, depending on the value of the element.
If the element is 1, then we would push back 2.
Otherwise, we just push back the original value.
And there we are.
So we have a new vector in which 3, 4, 5, 9 are the same, but 1 is replaced by 2.
And there is also a copy version of replace_if(). It is called replace_copy_if(). So this will take
the iterator range from the original vector.
The destination iterator. The lambda expression.
And if this lambda expression returns true, the element in the new vector will have the value
six. Otherwise, it have the same value that it does in "vec1".
So there it is, replace_copy_if().
The lambda expression: if the number is even, it will have the value 6, otherwise it will have the original
value.
And again, we could write a loop with if statements and push_backs.
So there we are, we have the even value 4 in the original vector and in the new vector, that is
six.
But the other elements all have the same values.
OK, so that is it for this video.
I will see you next time.
But until then, keep coding!