class Person
{
public:
int age;
};
I want to store objects of the class Person in a priority queue.
priority_queue< Person, vector<Person>, ??? >
I think I need to define a class for the comparison thing, but I am not sure about it.
Also, when we write,
priority_queue< int, vector<int>, greater<int> >
,
How does the greater<int>
work?
EDIT:
I found the operator overloading technique quite easy as mentioned by @andbluer. However there is another method I found.
We would write a comparator class, for example:
struct CompareAge {
bool operator()(Person const & p1, Person const & p2) {
// return "true" if "p1" is ordered before "p2", for example:
return p1.age < p2.age;
}
};
and use that as the comparator argument:
priority_queue<Person, vector<Person>, CompareAge>
What does bool operator()
mean here? We are overloading ‘()’? How does it work?