Using STL priority_queue
, I get an "invalid heap" error as soon as I try to use pop()
. I can push my values ββinto the queue, top()
queues are what I would expect and affordable. pop()
when it moves to re-heap, it seems to be a problem.
I keep pointers to the template class in the queue. I compared the overload:
template <class type>
class vertexPriorityCompare
{
public:
bool operator()(Vertex<type>* leftVertex, Vertex<type>* rightVertex) const
{
if(leftVertex->getDistanceFromSource() < 0 && rightVertex->getDistanceFromSource() < 0)
{
return false;
}
else if(leftVertex->getDistanceFromSource() < 0)
{
return true;
}
else if(rightVertex->getDistanceFromSource() < 0)
{
return false;
}
else
{
return leftVertex->getDistanceFromSource() > rightVertex->getDistanceFromSource();
}
}
};
priority_queue
is a private member of the class:
priority_queue< Vertex<type>*, vector< Vertex<type>* >, vertexPriorityCompare<type> > Q;
Overloading works in its own way, because the negative distance is considered infinity, always greater than anything else; to represent infinity, distances are initialized to -1. The queue should contain the smallest, but non-negative at the top.
, ? , , ?
, , , , . , .
, , , , priority_queue
, , , , , Vertex<type>
.
Visual Studio 2008 "stdthrow.cpp" 24.