levarko's blog

By levarko, 12 months ago, In English

here's something you should always remember. It will save you some time debugging

COMPARATORS MUST RETURN FALSE IF BOTH ARE EQUAL

although it may not seem logical at first, custom-made comparators MUST return false, if a == b, because they must follow a strict weak ordering

One of the properties of the strict weak ordering is the following: Nothing is considered less than itself. That means that:

if (a > b) return True
if (a == b) return False
if (a < b) return False

If you'll somehow forget about it, there are always std::greater<> and std::less<> waiting for you to use them

Others include

  • std::less_equal<>

  • std::greater_equal<>

  • std::equal_to<>

  • std::not_equal_to<>

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it