WEBVTT

00:00.150 --> 00:06.270
Hello again! In this video, we are going to look at algorithms which have underscore if versions. Many

00:06.270 --> 00:08.430
algorithms actually come in two versions.

00:08.730 --> 00:14.640
There is a base version which takes a value argument, and another version with underscore if after the

00:14.640 --> 00:17.310
name, which takes a predicate argument.

00:18.570 --> 00:23.730
We know about the find algorithm, which takes a value and searches for the first element which

00:23.730 --> 00:24.870
is equal to that value.

00:25.650 --> 00:33.200
There is also a find underscore if algorithm, which takes a predicate function, and it will call

00:33.210 --> 00:38.940
this predicate function on each element in turn, until it finds an element for which the predicate

00:38.940 --> 00:42.930
returns true. And then it will return an iterator to that element.

00:45.470 --> 00:47.930
We need to provide this predicate function.

00:48.200 --> 00:51.200
So it is going to be called with a single argument and it returns bool.

00:52.370 --> 00:54.650
One way we can do this is with a functor.

00:58.290 --> 01:04.770
So here is a functor class. It is called "ge5", for reasons which will become apparent!

01:06.360 --> 01:11.670
It just has one member, which is a public function. The function call operation.

01:12.360 --> 01:15.840
This takes a single string as argument and returns bool.

01:17.010 --> 01:20.790
And it will return true if the argument has more than 5 characters in it.

01:23.500 --> 01:25.870
Then we have our vector of names again.

01:26.740 --> 01:29.920
And first we are going to do this by writing a manual loop.

01:30.400 --> 01:34.720
So we are going to iterate over the vector elements using a range for loop.

01:35.380 --> 01:39.880
And then we call the functor, with each of these elements as its argument.

01:40.150 --> 01:43.450
So we create an object of the functor, outside the loop.

01:45.400 --> 01:49.360
And then, when this function returns true, the loop will end.

01:52.780 --> 01:58.960
I have also written an algorithm call. So this is going to take the begin() and end() of the vector as

01:58.960 --> 02:02.620
arguments, and it is going to be passed an object of this functor.

02:03.310 --> 02:06.460
Well, we could actually use that one, but it does not really matter.

02:07.540 --> 02:14.290
So find_if() it will call if this functor with each element in turn, as the argument. And when it finds

02:14.290 --> 02:18.090
one which returns true, then the find_if() call it will return.

02:18.090 --> 02:19.540
An iterator to that element.

02:20.710 --> 02:25.630
Then we can check if we have a valid element. If we found a match. And then we can do something with

02:25.630 --> 02:26.320
that iteration.

02:30.310 --> 02:34.180
So there we are, the first name with greater then 5 characters is "Dilbert".

02:36.380 --> 02:36.860
Yes.

02:37.370 --> 02:40.640
Perhaps I should have put them in a different order. Never mind!

02:42.190 --> 02:47.530
So you can imagine the code for the find_if() algorithm looks like this. So we have a pair of iterators,

02:47.530 --> 02:49.030
the iterator range.

02:49.480 --> 02:53.610
We have some callable function and we return another iterator.

02:54.460 --> 02:55.780
Then it loops over the elements.

02:56.140 --> 03:00.220
It calls the function with the current element as the argument.

03:00.910 --> 03:07.180
And then if that is true, then it returns that iterator and ends the loop. If it gets to the end of

03:07.180 --> 03:11.170
the loop without finding a match, then it returns the end of the range iterator.

03:13.840 --> 03:18.970
Incidentally, there is a find_if_not() algorithm, so we can exactly the same code.

03:19.510 --> 03:25.150
And this will look for an element which causes the predicate to return false.

03:25.720 --> 03:30.020
So this is going to call the functor for every element in the vector.

03:30.700 --> 03:36.130
And when it finds one with fewer than 5 characters, it will return an iterator to that element.

03:37.690 --> 03:38.870
So let's try this out.

03:43.360 --> 03:47.410
So the first word with fewer than 5 characters is "PHB".

03:51.780 --> 03:57.660
Let's suppose now that we want to generalize this. So being able to look for 5 characters is good,

03:57.660 --> 04:03.300
but we would like to be able to look for any number of characters. And we can do that by adding state

04:03.300 --> 04:04.260
to our functor.

04:04.920 --> 04:09.600
So we now call it "ge_n", so it is greater than or equal to "n" characters.

04:10.320 --> 04:16.710
We have this member "n", which is the number of characters we are looking for, and we set this in

04:16.710 --> 04:17.460
the constructor.

04:18.750 --> 04:24.900
And then in the operator, we just compare the number of characters in the data to this value "n".

04:28.050 --> 04:33.990
When we call the find_if() algorithm, we need to provide an object, and we need to pass the value of this

04:33.990 --> 04:35.970
number "n" to the constructor.

04:36.570 --> 04:38.820
So that is now an object with 5 as the argument.

04:39.210 --> 04:43.380
So that is going to do the same thing as the one we had before. It is going to find the first element with

04:43.380 --> 04:44.760
more than 5 characters in it.

04:49.200 --> 04:50.490
And it is "Dilbert" again.

04:52.650 --> 04:54.810
So let's just change these around, slightly.

05:05.290 --> 05:12.280
(So it should still be - actually let's change those around.) Just to prove it is not printing out "Dilbert" all the time!

05:16.860 --> 05:20.180
So there we are. The first word with more than 5 characters is "Dogbert".

05:22.380 --> 05:24.840
And then we can change that. So we make that 3.

05:30.480 --> 05:32.700
So the first one is "Asok".

05:32.920 --> 05:35.120
(I think it is pronounced "Ashok", actually.)

05:35.730 --> 05:37.050
And you can experiment with that.

05:37.770 --> 05:40.470
(Okay, so let's just put that back to actually how it was before.)

05:43.650 --> 05:45.600
Okay, so that is it for this video.

05:46.080 --> 05:46.910
I'll see you next time.

05:47.130 --> 05:48.870
Until then, keep coding!
