EbraM96's blog

By EbraM96, history, 7 years ago, In English
  • Vote: I like it
  • -1
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Your comparison function is incorrect. For example it returns true for 0 and 0 which should never happen for correct less than comparison.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    That's true but what's the problem ? If two equal values are swapped in the array it will cauz no effect.

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Well first of all, even this is already enough according to C++ standard to lead to undefined behaviour so you can't expect anything after doing sort like that. But in reality I think your comparison function acts very weird in other cases too, like:

      2<0:true
      0<1:true
      1<2:true
      

      I don't think it can be well defined what sort should do in this case.

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      C++ assumes that your comparator is strict "less than" operator, not the "less than or equal to". So the followings must be satisfied, otherwise you'll get weird result.

      1) if func(a, b) is true, then func(b, a) must be false 2) if func(a, b) and func(b, c) is true, then func(a, c) must be true 3) func(a, a) must be false