If you just want your class be comparable for using std::sort
and std::priority_queue
, overloading operator<
is just fine. As stated here
comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than (i.e. is ordered before) the second.
operator<
function does satisfy the requierment. Also the elements are compared by operator<
if custom comparator is not specified.
template< class RandomIt > void sort( RandomIt first, RandomIt last );
(1)
- Elements are compared using
operator<
.
Same applies to std::priority_queue
.
So in your case, you can do just with operator<
, if your only requierment is to use std::sort
and std::priority_queue
.
However, if you are going to reverse the order with std::greater
, you will need to implement operator>
. As stated here
Function object for performing comparisons. Unless specialized, invokes operator>
on type T.
However, if you are going only to use std::greater
to sort the class. You can do it by a little trick. If you implement operator<
as it was operator>
, it will do what is intended.