简体   繁体   English

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

[英]Implementing a priority queue that can be iterated over in C++

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. 我需要为项目实现优先级队列,但是没有指示STL的priority_queue因为我们需要迭代所有元素并随机删除它们。

We are thinking about using the STL's set for this, wrapping it in a class to make it an ADT. 我们正在考虑使用STL的set ,将它包装在一个类中以使其成为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? 我们怎样才能使它所以有些set的公共成员函数可以公开使用? We're interested in iterators, etc. 我们对迭代器等感兴趣

Apparently deriving the STL is unwise because of the lack of virtual destructors :/ 由于缺少虚拟析构函数,显然导出STL是不明智的:/


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() : 编译器不会抱怨插入,但它确实抱怨erase(begin())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? 为什么是这样?

You should be able to implement your own priority queue using std::vector , std::make_heap , std::push_heap , and std::pop_heap . 您应该能够使用std::vectorstd::make_heapstd::push_heapstd::pop_heap实现自己的优先级队列。 Isn't this how std::priority_queue is implemented? 这不是std::priority_queue是如何实现的? You'll just need to call std::make_heap again to fix the data structure when you remove a random element. 当你删除一个随机元素时,你只需要再次调用std::make_heap来修复数据结构。

Do you need to iterate over the elements in order? 你需要按顺序迭代元素吗? There's a std::sort_heap algorithm to order the underlying std::vector . 有一个std::sort_heap算法来排序底层的std::vector

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). 如果需要对列表进行排序,请在开头对其进行排序,然后在插入新项时使用插入排序(在正确的位置插入新项)。

STL's set should be usable to make what you want, although I must note that the list of requirements looks a little strange. STL的设置应该可以用来制作你想要的东西,虽然我必须注意到要求列表看起来有点奇怪。 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
};

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. 您可以从使用现有Container之一的合成开始,并在必要时从那里构建。

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

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