Help in priority_queue with comparator.

Правка en1, от Pie-Lie-Die, 2020-05-15 15:53:46

For an array, if we want to sort in descending order, we can use the greater() comparator. What it means is that the greater element should come before smaller element. But, in case of priority_queue in C++, if we use greater() as comparator, it makes the top element the smallest. So, if I write something like following:-

while(!pq.empty())     // pq is priority_queue of integers
{
   auto c = pq.top();
   pq.pop();
   cout << c << " ";
}

Above code results in integers being printed in ascending order. Can someone explain why is that so? Or does comparator have different meaning in case of priority_queue? For a fact, I know that by default, the priority queue in C++ is max_heap. Does this have to do anything with it? I searched on internet but couldn't find valid reason for this. Thanks.

Теги #help, priority queue, #codeforces

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский Pie-Lie-Die 2020-05-15 15:53:46 879 Initial revision (published)