I found the constructor of priority_queue on http://www.cplusplus.com/reference/queue/priority_queue/priority_queue/ is like this :
priority_queue (const Compare& comp, const Container& ctnr);
but the example I found is like this:
std::priority_queue<int, std::vector<int>, std::greater<int> > q2;
What's the difference between these two constructors?
I have tried both of them on my own, but the first one didn't work, the priority_queue wasn't sorted from small to large. Here is the code:
priority_queue<greater<int>, vector<int>> pq;
pq.push(4);
pq.push(2);
pq.push(1);
pq.push(3);
pq.push(5);
for (int i = 0; i < 5; i++) {
cout << pq.top() << endl;
pq.pop();
}
The result is still 5, 4, 3, 2, 1
What's the difference between these two constructors?
One of them is a constructor; the other is not.
The line beginning typedef
just creates a type alias, called mypq_type
. You would still pass those same constructor arguments when you create an object of this mypq_type
type.