Using the STL's priority_queue
I get the error "invalid heap" as soon as I try to use pop()
. I can push my values into the queue, the top()
of the queue is what I would expect and accessible. pop()
, when it goes to re-heap, seems to have a problem.
I am storing pointers to a templated class in the queue. I have the comparision overloaded:
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();
}
}
};
The priority_queue
is a private member of a class:
priority_queue< Vertex<type>*, vector< Vertex<type>* >, vertexPriorityCompare<type> > Q;
The overload works in the fashion it does, because a negative distance is considered infinity, always larger than whatever else; to represent infinity, distances are initialized to -1. The queue needs to keep the smallest, but non-negative at the top.
I dereference the pointers in the overload, is what I'm doing there allowable? And, is there another operator I need to overload?
I would attach code, but it seems if I do, it scares people away. Request to see more and I'll attach to another message.
I dynamically declare an array of pointers to pointers, these are what get pushed, because I assume priority_queue
stores by reference, so if I just put a pointer declared in the loop into the queue, that pointer goes out of scope. These pointers point to the proper Vertex<type>
, and exist throughout the function.
Visual Studio 2008 debugger takes me into 'stdthrow.cpp' line 24.