-3

Why couldn't the greater operator be used as the comparison? Is there a particular advantage in using less than?

  • 2
    All the other STL facilities use less-than. Consequently, a lot of classes are written that implement `operator<` but not necessarily `operator>`. If `priority_queue` weren't consistent, it won't be usable with such classes without extra work. So basically, it's to minimize the size of the interface that the user needs to provide in order to use the library. – Igor Tandetnik Nov 25 '18 at 17:00
  • 2
    There is no advantage, technically. It's just for consistency. Besides, the minimum requirement for sorting is defined with by some ordering relationship, and the STL chose some 'form of` operator <` – WhiZTiM Nov 25 '18 at 17:01
  • If you want to be equal opportunity for all operators, check out the boost class template boost::less_than_comparable https://stackoverflow.com/questions/4589353/what-is-the-minimum-set-of-operators-i-need-to-overload/4589464?s=2|37.0776#4589464 – natersoz Nov 25 '18 at 17:06
  • 1
    As long as the operators implement/impose a strict weak ordering, `std::less` and `std::greater` could each be the default. But, you have to pick one, and less seems more natural most of the time, so it's a sane default. You can always override it if you need to. – Jesper Juhl Nov 25 '18 at 17:14

1 Answers1

4

There are two aspects to your question:

  • Firstly, that's technically not correct: Most algorithms (not sure which one exactly you mean) use std::less as a default. However, that default can be overridden. The important point is the behaviour of the comparison, i.e. that it imposes a "strict-weak ordering", a term you will find mentioned in many texts on the topic and (I believe) even the standard itself.
  • Secondly, doing things in ascending order is often "natural" and it is the default throughout the C++ standard library.
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55