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?

3
  • This is by design. operator< implies a max-heap. Commented Apr 23, 2020 at 8:08
  • @Evg why does that not apply to arrays? Commented Apr 23, 2020 at 8: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. Commented Apr 23, 2020 at 8:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.