0

I wrote a comparator to sort a priority queue in increasing order of the second term, and then increasing order of the first term:

struct Comp {
    bool operator() (pair<int, int> a, pair <int, int> b) {
        if (a.second!=b.second) 
        return a.second<b.second;
        else {
            return a.first<b.first;
        }
    }
};

Generally, if I were to write a comp function for an array struct we would use the less than "<" operator to sort in increasing order, but for this priority queue, I've had to use the > operator to make it print the elements correctly in increasing order through q.top(). Does a priority_queue's "sort" make the "top" element the largest element if I use the "<" operator or least element?

Bob Wang
  • 63
  • 4
  • This is by design. `operator<` implies a max-heap. – Evg Apr 23 '20 at 08:08
  • @Evg why does that not apply to arrays? – Bob Wang Apr 23 '20 at 08:19
  • It is just a convention used in the standard library. `std::sort` sort arrays in the ascending order, but `std::make_heap` puts the largest element first. But heap is not a sorted array. – Evg Apr 23 '20 at 08:25

0 Answers0