WEBVTT

00:00.180 --> 00:05.420
Hello again! In this video, we are going to look at the less-than operator. The less-than operator

00:05.430 --> 00:06.810
is a non-member function.

00:07.080 --> 00:09.720
It takes two arguments and it returns a bool.

00:10.410 --> 00:16.950
If the first argument, the object on the left, is less than the second argument, the object on the

00:16.950 --> 00:20.070
right, it returns true, otherwise false.

00:22.360 --> 00:25.150
This is actually a very important operator.

00:25.750 --> 00:31.030
The Standard Library uses the less-than operator to sort and order the elements in a container.

00:31.780 --> 00:36.570
If we have a container and the elements do not implement the less-than operator, then we cannot

00:36.610 --> 00:38.350
sort the elements in that container.

00:38.980 --> 00:44.740
If you try to sort it, the code does not compile and you get a huge, confusing template error!

00:46.640 --> 00:51.950
There are also some containers which sort their elements. They store their objects in an order

00:51.950 --> 00:57.650
which is sorted. If we try to insert an object which does not support the less-than operator into one

00:57.650 --> 00:58.460
of these containers,

00:58.910 --> 01:00.380
then again, that does not compile.

01:03.020 --> 01:06.290
You may wonder what the Standard Library does about other comparisons.

01:06.710 --> 01:11.090
And the answer is that you can generate any comparison operator from the less-than operator.

01:11.870 --> 01:17.870
For example, if a less-than b is false and b less-than a is also false, then a must be equal to

01:17.870 --> 01:18.170
b.

01:18.620 --> 01:20.240
So that is your equality operator!

01:21.530 --> 01:25.400
If a less-than b is false, then a must be greater than or equal to b.

01:26.390 --> 01:32.090
And if a less-than b is false and a is not equal to b, then a must be greater than b. And so on.

01:33.540 --> 01:38.030
So I have added the less-than operator to our student class.

01:42.750 --> 01:45.510
We are going to be using the name to start with.

01:46.410 --> 01:49.410
So this is going to compare the name members of the objects.

01:49.680 --> 01:51.180
These are standard strings.

01:51.480 --> 01:56.830
So this will call the less-than operator for the string class. That will perform an alphabetical sort,

01:56.850 --> 02:01.620
so a name at the beginning of the alphabet is less than a name which is later on in the alphabet.

02:02.190 --> 02:03.660
And it is also case sensitive.

02:04.200 --> 02:08.430
So a name with a capital letter is less than a name with a lower case letter.

02:10.710 --> 02:14.460
And the main function is the same, except we are using the less-than operator this time.

02:15.510 --> 02:16.470
So let's try that out.

02:18.090 --> 02:21.590
So remember, this is just looking at the names. It completely ignores the IDs.

02:21.600 --> 02:27.570
These John Smiths are regarded as having the same value by the less-than operator, so they are not less than

02:27.570 --> 02:27.990
each other.

02:28.680 --> 02:31.030
Jack Jones IS less than John Smith.

02:31.500 --> 02:39.330
So "stu3" is less than the other two, and John Smith is not less than Jack Jones, so everything

02:39.330 --> 02:40.200
else is false.

02:41.010 --> 02:42.420
So that all works as expected.

02:43.140 --> 02:48.450
By the way, if you define a less-than operator for a class and also the inequality operators, you should

02:48.450 --> 02:49.920
make these behave consistently.

02:50.580 --> 02:57.780
So if a is less than b, then a cannot be equal to b, and a must be not equal to b. If a is equal

02:57.780 --> 03:00.660
to be, then a cannot be less than b and b cannot be...

03:01.260 --> 03:03.060
So that is just something to bear in mind.

03:06.000 --> 03:12.540
Talking of sorting, the C++ library has a sort() function, std::sort, in the <algorithm> header.

03:13.020 --> 03:16.530
And this takes an iterator range and it will sort the elements in that range.

03:17.550 --> 03:22.290
For example, we can source all the elements in a vector, by passing the return values from begin()

03:22.290 --> 03:23.160
and end().

03:25.250 --> 03:32.300
Like that. And this will sort the elements in ascending order. The sort function will use

03:32.300 --> 03:35.270
the less-than operator of the elements to determine the order.

03:37.490 --> 03:42.350
If the elements are string, as they were with the students, then this is going to perform a sort in

03:42.350 --> 03:43.700
increasing alphabetical order.

03:44.720 --> 03:49.880
And we are using begin() and not cbegin() because the source algorithm will move the elements around.

03:50.180 --> 03:51.950
So the iterators cannot be constant.

03:56.730 --> 04:01.230
So here's an example of where we have a vector of names, of characters in a well-known cartoon.

04:02.640 --> 04:07.170
We start by printing out the elements using a range-for loop.

04:08.250 --> 04:09.930
Then we call our sort.

04:10.620 --> 04:14.880
So that is going to sort all the elements in the vector. And then we print out all the elements again.

04:15.150 --> 04:17.520
So these should be sorted into alphabetical order.

04:19.800 --> 04:20.700
And that is what we get.

04:20.730 --> 04:24.600
So that is the vector before, and that is the vector after, in alphabetical order.

04:27.480 --> 04:32.820
As with the equality operator, you may need to think about what it means for one of your objects to

04:32.820 --> 04:37.210
be less than another one. With the student class we have two possibilities.

04:37.860 --> 04:44.610
We can use the names, so we can use alphabetical ordering to decide which comes first.

04:45.420 --> 04:50.520
And that might be more convenient if you're a teacher or lecturer and you want a list of students in

04:51.090 --> 04:57.180
order of their names. You could also use the ID, to order the elements.

04:57.630 --> 05:00.540
And that will sort the students in numerical order.

05:01.380 --> 05:06.040
And that might be more convenient if you are an administrator for the school or college or university

05:06.040 --> 05:06.540
or whatever.

05:08.530 --> 05:12.430
So going back to our student class again. This is all the same.

05:15.490 --> 05:21.880
And then in the main function, we are going to create a vector of these students. And then we are going

05:21.880 --> 05:24.580
to sort it, so we put them out before and after, again.

05:28.520 --> 05:30.240
So we have these in alphabetical order.

05:30.260 --> 05:36.560
We have Jack Jones first and then the John Smith's. And again, the sort does not take the ID into

05:36.560 --> 05:37.280
consideration.

05:37.820 --> 05:39.320
So these could be the other way around.

05:45.510 --> 05:48.630
If we sort by the ID, so we need to change this.

05:58.460 --> 06:02.480
(The problem with comments is they are not checked by the compiler, so they easily get out of date)

06:03.860 --> 06:08.570
And we want to make sure that the equality operator is consistent, so we use the ID there as well.

06:09.080 --> 06:12.980
We do not actually call it in this program, but we might use this code later in some way where we do

06:12.980 --> 06:14.450
need the equality operator.

06:16.040 --> 06:17.990
So let's try calling our sort again.

06:20.200 --> 06:20.920
And there you are.

06:20.950 --> 06:26.920
Now they are ordered by the ID. So John Smith with the lowest ID comes first and then Jack Smith

06:26.920 --> 06:30.670
comes last because he has the highest ID. Sorry, Jack Jones.

06:30.670 --> 06:31.330
Not Jack Smith.

06:32.950 --> 06:34.900
Okay, so that's it for this video.

06:35.380 --> 06:36.190
I'll see you next time.

06:36.190 --> 06:38.230
But until then, keep coding!
