c++ custom compare function for std::sort()

前端 未结 3 916
滥情空心
滥情空心 2020-12-01 10:42

I want to create custom compare function for std::sort(), to sort some key-value pairs std::pair

Here is my function

 template 

        
3条回答
  •  温柔的废话
    2020-12-01 11:05

    Your comparison function is not even wrong.

    Its arguments should be the type stored in the range, i.e. std::pair, not const void*.

    It should return bool not a positive or negative value. Both (bool)1 and (bool)-1 are true so your function says every object is ordered before every other object, which is clearly impossible.

    You need to model the less-than operator, not strcmp or memcmp style comparisons.

    See StrictWeakOrdering which describes the properties the function must meet.

提交回复
热议问题