如何从priority_queue中删除不在顶部的元素?

时间:2013-10-19 15:06:33

标签: c++ stl priority-queue binary-heap

在我的程序中,我需要从不在顶部的优先级队列中删除一个元素。可以这样做吗?如果没有,请建议一种方法,除了创建自己的堆。

5 个答案:

答案 0 :(得分:24)

标准priority_queue<T>可以通过继承进行自定义。它具有可以在后代类中引用的受保护成员ccomp

template<typename T>
class custom_priority_queue : public std::priority_queue<T, std::vector<T>>
{
  public:

      bool remove(const T& value) {
        auto it = std::find(this->c.begin(), this->c.end(), value);
        if (it != this->c.end()) {
            this->c.erase(it);
            std::make_heap(this->c.begin(), this->c.end(), this->comp);
            return true;
       }
       else {
        return false;
       }
 }
};

void main()
{
   custom_priority_queue<int> queue;

   queue.push(10);
   queue.push(2);
   queue.push(4);
   queue.push(6);
   queue.push(3);

   queue.remove(6);

   while (!queue.empty())
   {
      std::cout << queue.top();
      queue.pop();

      if (!queue.empty())
      {
        std::cout << ", ";
      }
   }

 }

输出:

10,4,3,2

答案 1 :(得分:4)

Pradip和MASh牺牲了实现删除操作的时间。 但是如果时间复杂度对你很重要,我建议你使用hash min_heap。 哈希表存储值指针,指针指向min_heap。 这意味着您可以花费O(1)时间在min_heap中找到值,并使用O(log(n))来移除(筛选或筛选)元素。

答案 2 :(得分:3)

一个处理优先级队列STL删除的巧妙小技巧-使用另一个优先级队列,例如del_pq。继续插入所有删除值。当您从原始优先级队列中弹出值时,请在del_pq的顶部进行检查,看看是否要删除它。如果匹配,请从原始的priority_queue中删除该值。

此方法实现了一种方法来延迟删除原始优先级队列中的值。可以占用两倍的内存,但平均删除和插入仍为O(logN)

答案 3 :(得分:0)

最好的解决方案是使用std :: set。集合提供的方法允许将其用作最小/最大堆(或优先级队列)。

std::set<int> pq;

//accessing the smallest element(use as min heap)
*pq.begin();

//accessing the largest element (use as max heap)
*pq.rbegin();

此外,还可以进行随机删除。

//to delete the integer '6'
auto it = pq.find(6);
pq.erase(it);

答案 4 :(得分:-3)

您想要删除priority_queue<type> Q中的第5个元素。 然后你就可以这样做:

vector<type> tempQ;
int i=0;
int n=5;
type t;
while(i<n-1)
{
    tempQ.push_back(Q.top());
    Q.pop();        
    i++;
}
Q.pop();
i=0;
while(i<n-1)
{
    t=tempQ[i++];
    Q.push(t);
}