0

I was trying to create a minHeap using the regular syntax but found out that

maxHeap is initialised as:

priority_queue<int> maxHeap;

whereas minHeap is initailised as:

priority_queue<int, vector<int>, greater<int>> minHeap;

Any idea to why is it so in C++? It could have been much easier to remember if minHeap and maxHeap were initialised using similar syntax.

theebugger
  • 77
  • 9
  • 1
    The 2nd and 3rd template parameters have defaults see: https://en.cppreference.com/w/cpp/container/priority_queue As you can't just supply the 3rd parameter (compare) you have to supply the 2nd (container type) as well. – Richard Critten May 01 '21 at 16:14

1 Answers1

2

The priority_queue defaults to a max heap, so priority_queue<int> is shorthand for priority_queue<int, vector<int>, less<int>>, which does indeed match the min heap syntax

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158