简体   繁体   English

提升优先级队列,查看元素是否已在队列中

[英]boost priority queue, see if element is already in the queue

I need the above functionality for pathfinding. 我需要上述功能进行寻路。

I'm using the boost queue as it apparently allows me to do this. 我正在使用加速队列,因为它显然允许我执行此操作。

I have my queue setup like so: 我的队列设置如下:

typedef boost::heap::priority_queue<PathFindingNode *, boost::heap::compare<MyClassCompare> > MyPriorityQueue;

I want to create a method that takes in type PathFindingNode as a parameter, then checks to see if this Node is already in the queue. 我想创建一个将PathFindingNode类型作为参数的方法,然后检查该节点是否已在队列中。

I found I could iterate through the queue like this: 我发现我可以像这样遍历队列:

typename MyPriorityQueue::iterator begin = self.queue->begin();
typename MyPriorityQueue::iterator end = self.queue->end();

for (typename MyPriorityQueue::iterator it = begin; it != end; ++it)
{
}

But I don't understand how to use the it value to compare against a node passed into the method block? 但是我不明白如何使用it值与传递给方法块的节点进行比较?

If you iterate over the queue each time, you'll kill the performance guarantees of your algorithm. 如果每次都循环访问队列,则会破坏算法的性能保证。 Instead, you should store the handle returned from the queue upon adding a new entry into the queue and use the handle to potentially update the priority of the entry. 而是应在将新条目添加到队列中时存储从队列返回的句柄,并使用该句柄来潜在地更新条目的优先级。 You might need to use one of the node-based priority queues, however. 但是,您可能需要使用基于节点的优先级队列之一。

You could eg use the Fibonacci Heap whose push() yields a handle_type . 您可以使用斐波那契堆 ,例如,其push()生成handle_type You'd store this value with your node and later use with increase() or decrease() to adjust the priority of the node in the heap. 您可以将此值存储在节点中,以后与increase()decrease()以调整堆中节点的优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM