Hello again! In this video, we are going to look at algorithms which have underscore if versions. Many
algorithms actually come in two versions.
There is a base version which takes a value argument, and another version with underscore if after the
name, which takes a predicate argument.
We know about the find algorithm, which takes a value and searches for the first element which
is equal to that value.
There is also a find underscore if algorithm, which takes a predicate function, and it will call
this predicate function on each element in turn, until it finds an element for which the predicate
returns true. And then it will return an iterator to that element.
We need to provide this predicate function.
So it is going to be called with a single argument and it returns bool.
One way we can do this is with a functor.
So here is a functor class. It is called "ge5", for reasons which will become apparent!
It just has one member, which is a public function. The function call operation.
This takes a single string as argument and returns bool.
And it will return true if the argument has more than 5 characters in it.
Then we have our vector of names again.
And first we are going to do this by writing a manual loop.
So we are going to iterate over the vector elements using a range for loop.
And then we call the functor, with each of these elements as its argument.
So we create an object of the functor, outside the loop.
And then, when this function returns true, the loop will end.
I have also written an algorithm call. So this is going to take the begin() and end() of the vector as
arguments, and it is going to be passed an object of this functor.
Well, we could actually use that one, but it does not really matter.
So find_if() it will call if this functor with each element in turn, as the argument. And when it finds
one which returns true, then the find_if() call it will return.
An iterator to that element.
Then we can check if we have a valid element. If we found a match. And then we can do something with
that iteration.
So there we are, the first name with greater then 5 characters is "Dilbert".
Yes.
Perhaps I should have put them in a different order. Never mind!
So you can imagine the code for the find_if() algorithm looks like this. So we have a pair of iterators,
the iterator range.
We have some callable function and we return another iterator.
Then it loops over the elements.
It calls the function with the current element as the argument.
And then if that is true, then it returns that iterator and ends the loop. If it gets to the end of
the loop without finding a match, then it returns the end of the range iterator.
Incidentally, there is a find_if_not() algorithm, so we can exactly the same code.
And this will look for an element which causes the predicate to return false.
So this is going to call the functor for every element in the vector.
And when it finds one with fewer than 5 characters, it will return an iterator to that element.
So let's try this out.
So the first word with fewer than 5 characters is "PHB".
Let's suppose now that we want to generalize this. So being able to look for 5 characters is good,
but we would like to be able to look for any number of characters. And we can do that by adding state
to our functor.
So we now call it "ge_n", so it is greater than or equal to "n" characters.
We have this member "n", which is the number of characters we are looking for, and we set this in
the constructor.
And then in the operator, we just compare the number of characters in the data to this value "n".
When we call the find_if() algorithm, we need to provide an object, and we need to pass the value of this
number "n" to the constructor.
So that is now an object with 5 as the argument.
So that is going to do the same thing as the one we had before. It is going to find the first element with
more than 5 characters in it.
And it is "Dilbert" again.
So let's just change these around, slightly.
(So it should still be - actually let's change those around.) Just to prove it is not printing out "Dilbert" all the time!
So there we are. The first word with more than 5 characters is "Dogbert".
And then we can change that. So we make that 3.
So the first one is "Asok".
(I think it is pronounced "Ashok", actually.)
And you can experiment with that.
(Okay, so let's just put that back to actually how it was before.)
Okay, so that is it for this video.
I'll see you next time.
Until then, keep coding!