1
votes

I have declared std::priority_queue like this.

priority_queue < Aircraft, vector<Aircraft>, less<Aircraft> > *q;

And I overloaded the less operator like this.

bool Aircraft::operator<(const Aircraft &rhs) const
{
    return (m_dep_time < rhs.m_dep_time);
}

I printed out the priority queue and I was getting element in decreasing order (the largest dep_time first). I had to change m_dep_time < rhs.m_dep_time to m_dep_time > rhs.m_dep_time and it worked. I was able to get the lowest dep_time as I intended.

I thought in logically using m_dep_time < rhs.m_dep_time was right instead of m_dep_time > rhs.m_dep_time to get an element from the queue in increasing order, the lowest value first.

Could you explain what I am missing?

3

3 Answers

3
votes

Check out this reference. By default, the top element is the largest element. If you want opposite behavior, you must use different comparison function, just as you've done.

1
votes

The < in the queue describes the less-relationship on the priority. So if you want the element with the lowest time value to be the first (highest priority), you will have to compare the time as m_dep_time > rhs.m_dep_time