Search code examples
c++stlpriority-queue

Using STL priority_queue with greater_equal comparator class


Running the following example I receive a debug assertion in the marked line.

std::priority_queue<int, std::vector<int>, std::greater_equal<int>> queue_int;
queue_int.push(1);
queue_int.push(2);
queue_int.push(1); // Debug Assertion Failed: Expression invalid comparator

Any hints? Help much appreciated!


Solution

  • All the time when you are using a STL data structure with a comparator, that comparator needs to be strict and never return true if it is receiving equal object to compare.

    Imagine the case when 2 equal objects are compared, swapped and the next comparation will be again between the same 2 objects. In this case the STL sort step will never stop.

    Try std::greater instead of std::greater_equal