Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

less or less_equal using set

Tags:

c++

set

stl

We can pass a function as <(less) operator to STL data structures such as set, multiset, map, priority_queue, ...

Is there a problem if our function acts like <=(less_equal)?

like image 714
a-z Avatar asked Apr 29 '12 11:04

a-z


1 Answers

Yes, there is a problem.

Formally, the comparison function must define a strict weak ordering, and <= does not do that.

more specifically, the < is also used to determine equivalence (x and y are equivalent iff !(x < y) && !(y < x)). This does not hold true for <= (using that operator would have your set believe that objects are never equivalent)

like image 112
jalf Avatar answered Nov 07 '22 07:11

jalf