when we declare priority_queue<Person,vector, PersonCompare >pq; toh ye max heap act kar raha hai but
last video mai sir ne priority_queuepq_max;//default max priority queue
isko max queue bola tha and ye priority_queue<int, vector, greater >pq;//min priority queue bola tha toh yaha bhi pq min priority hona chaiye thaa naa?
Priority queue for custom class
Hey @jatinupadhyay786
It does not work like that, since u created PersonCompare function on your own so implementation of PersonCompare function decides whether it will be a min priority queue or Max priority Queue.
priority_queue<int, vector, greater >pq;
This is min priority queue because greater is implemented like that.
when we use priority_queue<int> pq; then its by default priority_queue<int,vector,less> pq;
ye samj gaya par
class PersonCompare{
public:
bool operator()(Person A, Person B){
return A.age < B.age;
}
};
ye compare function samjh nahi aaya mujhe hume max age chaiye par hum return true karwa rahe hai jaab A ki age is less than B ki age ?
Hey @jatinupadhyay786
This is how they are implemented in general.
In sorting its ascending order but in Priority queue we don’t sort we use heapify operation and comparison function is used to decide when we want to swap and when we don’t.