C ++ standard library template standard queue throws an exception with the message "Invalid heap"

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.

+3
5

, . , , :

bool operator()(...)
{
  return leftVertex<rightVertex;
}

, , . "- " . , , , , .

+3

, , getDistanceFromSource() , . ? , getDistanceFromSource(), ?

+3

?

, - , . , , , .

.

,

0

callstack , , Vertex<...>, Vertex<...> , Vertex<...>* .

0

:

, , priority_queue , , , . .

, . , , .

The queue is not stored by reference, it stores copies, but in this case, what you put in is pointers, so it copies a pointer that will point to the original objects that you selected.

I think we need a short complete example to get further.

0
source

Source: https://habr.com/ru/post/1702163/


All Articles