Search code examples
c++vectorpriority-queuemin-heapmax-heap

Why is maxHeap initialisation syntax different from minHeap through priority_queue?


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.


Solution

  • 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