jeudi 29 décembre 2016

C++ priority queue constructor

I found the constructor of priority_queue on http://ift.tt/1fbTFHc is like this : priority_queue (const Compare& comp, const Container& ctnr);, but the example it gives us below is like this: typedef std::priority_queue<int,std::vector<int>,mycomparison> mypq_type;. 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

Aucun commentaire:

Enregistrer un commentaire