相关疑难解决方法(0)

实现可以在C++中迭代的优先级队列

我需要为项目实现优先级队列,但是priority_queue没有指示STL,因为我们需要迭代所有元素并随机删除它们.

我们正在考虑将STL set用于此,将其包装在一个类中以使其成为ADT.

有更聪明的解决方案吗?

我们如何能够set公开使用某些公共成员职能呢?我们对迭代器等感兴趣

由于缺少虚拟析构函数,显然导出STL是不明智的:/


新代码:

#ifndef PRIORITYQUEUE_H_
#define PRIORITYQUEUE_H_

#include <set>

template<typename T, template<typename X> class impl_type = std::set>
class PriorityQueue {
    typedef impl_type<T> set_type;
    typedef typename set_type::iterator iterator;
public:
    void push(const T& x) {
        insert(x);

    }

    void pop() {
        erase(begin());
    }

    const T& top() const {
        return *begin();
    }
};

#endif /* PRIORITYQUEUE_H_ */
Run Code Online (Sandbox Code Playgroud)

所以,我们目前有这个.编译器不会抱怨插入,但它确实抱怨erase(begin())return *begin():

there are no arguments to 'begin' that depend on a template parameter, …

c++ priority-queue

5
推荐指数
1
解决办法
5202
查看次数

标签 统计

c++ ×1

priority-queue ×1