I couldn't find any information on how to order objects in a priority queue. I tried this:
class Person {
...
public:
bool operator<(const Person& p) {
return age < p.age;
}
}
int main() {
priority_queue<Person*> people;
people.push(new Person("YoungMan", 21));
people.push(new Person("Grandma", 83));
people.push(new Person("TimeTraveler", -5000));
people.push(new Person("Infant", 1));
while (!people.empty()) {
cout << people.top()->name;
delete people.top();
people.pop();
}
And it's supposed to give priority based on age (older people get higher priority, and thus leave the queue first), but it doesn't work. But I'm getting this output:
Infant
Grandma
TimeTraveler
YoungMan
And I have no idea what this is ordered by, but it's definitely not age.
priority_queue
of pointers, it's sorting using the value of the pointers not the age. Usepriority_queue<Person>
and remove thenew
s when you push.