mercredi 18 août 2021

Difference between compare order of std::sort and priority_queue (C++)

I am wondering why priority_queue and vector work the totally different way. Here is the sample:

priority_queue<int, vector<int>, less<int> > pq;
pq.push(1);
pq.push(2);
pq.push(3);
// if we print the elem in pq by pop(), we get 3, 2, 1
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(2);
std::sort(v.begin(), v.end(), less<int>()); 
// but we get 1, 2, 3 for vector

Both priority_queue and vector used less, but why the result is different?

Aucun commentaire:

Enregistrer un commentaire