WEBVTT

00:00.180 --> 00:07.920
Hello again! In this video, we are going to look at library function objects. The C++ library defines

00:07.920 --> 00:09.330
some function objects.

00:09.870 --> 00:16.950
So these are generic operators which perform all the usual arithmetic, logical and relational operations.

00:17.220 --> 00:21.030
So things like plus, greater than, not equals, and so on.

00:22.320 --> 00:24.860
These are implemented as functors.

00:24.900 --> 00:29.120
So these are classes which have a function call operator, and they are templated.

00:29.430 --> 00:30.930
So these are completely generic.

00:31.680 --> 00:34.710
They will work for any type which supports the underlying operator.

00:35.340 --> 00:35.850
All these do,

00:35.850 --> 00:38.580
is they just call the corresponding operator for the object.

00:38.940 --> 00:45.540
For example, with the less-than operator, which is actually called less. This will have a function

00:45.540 --> 00:53.190
call operator, which takes two arguments of the template type. And it returns a bool, which is the result

00:53.190 --> 00:55.200
of calling the less than operator.

00:57.330 --> 01:01.130
If we want to use this operator, we need to create an object.

01:01.170 --> 01:04.890
So we need to call the constructor or do something else which will create an object.

01:05.490 --> 01:11.790
We also need to give the paramater type. Although, in C++ 17, you can actually skip that.

01:12.750 --> 01:18.300
So this will create an object of this functor, and then the sort() algorithm will call the function

01:18.300 --> 01:22.170
call operator, which will in turn call the string's less-than operator.

01:25.430 --> 01:29.120
So let's have a look at this. So we have a vector of strings.

01:30.500 --> 01:37.310
We have our call to sort() with the function object as the arguments. Actually, let's remind ourselves

01:37.310 --> 01:39.740
what happens without that.

01:40.670 --> 01:43.190
So this is just the default ordering.

01:44.480 --> 01:47.420
So these are ordered in ascending alphabetical order.

01:47.420 --> 01:51.830
And that's because the sort() will use the string less-than operator by default.

01:58.840 --> 02:04.870
So if we put this back, it is now going to use the library less, for type string, and that will also

02:04.870 --> 02:07.100
use the less-than operator of the string class.

02:07.600 --> 02:12.610
So we should expect to get exactly the same results and that is what we do.

02:15.130 --> 02:18.170
There is something slightly more interesting if we use greater.

02:19.400 --> 02:22.790
So this will now call the greater-than operator of the string class.

02:23.360 --> 02:26.300
So we expect to get these sorted in the reverse order.

02:26.660 --> 02:30.470
So with the highest first, in reverse alphabetical order.

02:33.420 --> 02:33.720
OK.

02:33.810 --> 02:34.680
And that is what we get.

02:37.790 --> 02:41.480
So there is quite a few of these. They provide all the usual operations.

02:41.870 --> 02:44.750
It is pretty obvious what the object does.

02:45.380 --> 02:48.080
It may not always be obvious what the name of the object should be.

02:48.080 --> 02:50.960
I am not sure I would have chosen the same ones - but there you go.

02:52.100 --> 02:53.690
So those are for arithmetic.

02:54.470 --> 03:01.640
Then we have the relational operators: equal to, not equal to, greater, less, greater equal, less equal.

03:03.430 --> 03:13.000
And logical operators: logical and, logical or, logical not, and some bitwise operators as well. The

03:13.000 --> 03:18.490
not operator did not get in until C++ 14. For some reason.

03:19.270 --> 03:20.980
OK, so that's it for this video.

03:21.370 --> 03:24.340
I will see you next time, but until then, keep coding!
