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

发布于 2024-10-07 09:03:26 字数 980 浏览 0 评论 0原文

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

我们正在考虑为此使用 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_ */

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

there are no argument to ' begin'依赖于模板参数,因此'begin'声明必须可用

这是为什么?

I need to implement a priority queue for a project, but the STL's priority_queue is not indicated since we need to iterate over all elements and remove them randomly.

We are thinking about using the STL's set for this, wrapping it in a class to make it an ADT.

Is there a smarter solution for this?

How can we make it so some of set's public member functions can be used publicly? We're interested in iterators, etc.

Apparently deriving the STL is unwise because of the lack of virtual destructors :/


New code:

#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_ */

So, we currently have this. The compiler doesn't complain about insert, but it does complain about erase(begin()) and return *begin():

there are no arguments to 'begin' that depend on a template parameter, so a declaration of 'begin' must be available

Why is this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷了相思 2024-10-14 09:03:26

你真的需要优先级队列吗?

您需要迭代所有项目并随机删除 ->链表

如果需要保持列表排序,请在开头排序,然后在插入新项目时,使用插入排序(在正确的位置插入新项目)。

Do you really need a priority queue ?

You need iterate over all items and remove randomly -> linked list

If you need to keep the list sorted, sort it at the beginning and then, when inserting new item, use insertion sort (insert new item on right place).

感情洁癖 2024-10-14 09:03:26

您应该能够使用 std::vectorstd::make_heapstd::push_heap实现自己的优先级队列>std::pop_heap。这不是 std::priority_queue 的实现方式吗?当您删除随机元素时,您只需要再次调用 std::make_heap 来修复数据结构。

您需要按顺序迭代元素吗?有一个 std::sort_heap 算法来对底层 std::vector 进行排序。

You should be able to implement your own priority queue using std::vector, std::make_heap, std::push_heap, and std::pop_heap. Isn't this how std::priority_queue is implemented? You'll just need to call std::make_heap again to fix the data structure when you remove a random element.

Do you need to iterate over the elements in order? There's a std::sort_heap algorithm to order the underlying std::vector.

不交电费瞎发啥光 2024-10-14 09:03:26

STL 的集合应该可以用来制作你想要的东西,尽管我必须指出,需求列表看起来有点奇怪。您可以定义一个新类型。

template<typename T, template<typename X> class impl_type = std::set> class prio_queue {
    typedef impl_type<T> set_type;
    typedef typename set_type::iterator iterator;
    // etc
public:
    // Forward the set's members
    T& top() {
        return *begin();
    }
    // etc
};

STL's set should be usable to make what you want, although I must note that the list of requirements looks a little strange. You could just define a new type.

template<typename T, template<typename X> class impl_type = std::set> class prio_queue {
    typedef impl_type<T> set_type;
    typedef typename set_type::iterator iterator;
    // etc
public:
    // Forward the set's members
    T& top() {
        return *begin();
    }
    // etc
};
尘曦 2024-10-14 09:03:26

我将遵循标准库中其他一些容器适配器设置的示例使用组合,并将底层容器的类型作为模板参数。但由于这是一个学校项目,可能灵活性太大。您可以首先将组合与现有容器之一结合使用,并在必要时从那里进行构建。

I would follow the example set by some of the other container adapters in the standard library use composition and make the type of the underlying container a template parameter. Though since it is a school project that might be too much flexibility. You might start by using composition with one of the existing Containers and build from there if necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文