sir , when we are using inbuilt comparator greater in priority_queue.Then why we are using vector also ? Can i not do so without using vector?
Priority_queue when i want to make min heap and using greater <int>
Hey @gopi.jais0607
No, you can’t do so without mentioning vector in the argument.
Here’s the syntax for min priority queue/heap in stl:
priority_queue <int, vector, greater> pq
Actually we’ve to follow this syntax in max heap also but because of template parameters being positional and C++ standard commitee’s decision to order the containter type before the comparator type, we’ve to mention both vector and greater arguments.
For an example:
Just like with a function like this:
void foo(int a = 1, int b = 2);
- you can’t call it specifying b, but not a.
For max-heap, you’re using std::less, which happens to be the default, therefore you can omit the container type as well.
But for min-heap, you need to mention both.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.