Hello again! In this video, we are going to look at the less-than operator. The less-than operator
is a non-member function.
It takes two arguments and it returns a bool.
If the first argument, the object on the left, is less than the second argument, the object on the
right, it returns true, otherwise false.
This is actually a very important operator.
The Standard Library uses the less-than operator to sort and order the elements in a container.
If we have a container and the elements do not implement the less-than operator, then we cannot
sort the elements in that container.
If you try to sort it, the code does not compile and you get a huge, confusing template error!
There are also some containers which sort their elements. They store their objects in an order
which is sorted. If we try to insert an object which does not support the less-than operator into one
of these containers,
then again, that does not compile.
You may wonder what the Standard Library does about other comparisons.
And the answer is that you can generate any comparison operator from the less-than operator.
For example, if a less-than b is false and b less-than a is also false, then a must be equal to
b.
So that is your equality operator!
If a less-than b is false, then a must be greater than or equal to b.
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.
So I have added the less-than operator to our student class.
We are going to be using the name to start with.
So this is going to compare the name members of the objects.
These are standard strings.
So this will call the less-than operator for the string class. That will perform an alphabetical sort,
so a name at the beginning of the alphabet is less than a name which is later on in the alphabet.
And it is also case sensitive.
So a name with a capital letter is less than a name with a lower case letter.
And the main function is the same, except we are using the less-than operator this time.
So let's try that out.
So remember, this is just looking at the names. It completely ignores the IDs.
These John Smiths are regarded as having the same value by the less-than operator, so they are not less than
each other.
Jack Jones IS less than John Smith.
So "stu3" is less than the other two, and John Smith is not less than Jack Jones, so everything
else is false.
So that all works as expected.
By the way, if you define a less-than operator for a class and also the inequality operators, you should
make these behave consistently.
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
to be, then a cannot be less than b and b cannot be...
So that is just something to bear in mind.
Talking of sorting, the C++ library has a sort() function, std::sort, in the header.
And this takes an iterator range and it will sort the elements in that range.
For example, we can source all the elements in a vector, by passing the return values from begin()
and end().
Like that. And this will sort the elements in ascending order. The sort function will use
the less-than operator of the elements to determine the order.
If the elements are string, as they were with the students, then this is going to perform a sort in
increasing alphabetical order.
And we are using begin() and not cbegin() because the source algorithm will move the elements around.
So the iterators cannot be constant.
So here's an example of where we have a vector of names, of characters in a well-known cartoon.
We start by printing out the elements using a range-for loop.
Then we call our sort.
So that is going to sort all the elements in the vector. And then we print out all the elements again.
So these should be sorted into alphabetical order.
And that is what we get.
So that is the vector before, and that is the vector after, in alphabetical order.
As with the equality operator, you may need to think about what it means for one of your objects to
be less than another one. With the student class we have two possibilities.
We can use the names, so we can use alphabetical ordering to decide which comes first.
And that might be more convenient if you're a teacher or lecturer and you want a list of students in
order of their names. You could also use the ID, to order the elements.
And that will sort the students in numerical order.
And that might be more convenient if you are an administrator for the school or college or university
or whatever.
So going back to our student class again. This is all the same.
And then in the main function, we are going to create a vector of these students. And then we are going
to sort it, so we put them out before and after, again.
So we have these in alphabetical order.
We have Jack Jones first and then the John Smith's. And again, the sort does not take the ID into
consideration.
So these could be the other way around.
If we sort by the ID, so we need to change this.
(The problem with comments is they are not checked by the compiler, so they easily get out of date)
And we want to make sure that the equality operator is consistent, so we use the ID there as well.
We do not actually call it in this program, but we might use this code later in some way where we do
need the equality operator.
So let's try calling our sort again.
And there you are.
Now they are ordered by the ID. So John Smith with the lowest ID comes first and then Jack Smith
comes last because he has the highest ID. Sorry, Jack Jones.
Not Jack Smith.
Okay, so that's it for this video.
I'll see you next time.
But until then, keep coding!