随机访问优先队列

发布于 2024-10-07 02:51:40 字数 1607 浏览 0 评论 0原文

继续列表到优先级队列

我正在实现一个具有随机访问功能的改进的priority_queue。

template <class T, class Container = std::vector<T> >
class Heap {
public:
    Heap() {}

    Heap(const Container& container) {
        container_ = container;
        std::make_heap(container_.begin(), container_.end());
    }

    Heap<T, Container>& operator=(const Heap<T, Container>& heap) {
        if (this != &heap)
            container_ = heap.container_;

        return *this;
    }

    void push(const T& x) {
        container_.push_back(x);
        std::push_heap(container_.begin(), container_.end());
    }

    void pop() {
        std::pop_heap(container_.begin(), container_.end());
        container_.pop_back();
    }

    const T& top() {
        return container_.front();
    }

    const Container& getContainer() const {
        return container_;
    }

    T& operator[](size_t n) {
        return container_[n];
    }

    typename Container::const_iterator begin() const {
        return container_.begin();
    }

    typename Container::const_iterator end() const {
        return container_.end();
    }

    size_t size() const {
        return container_.size();
    }

    T& base() {
        return container_.back();
    }

    Container::iterator erase(Container::iterator position) {
        return container_.erase(position);
    }

private:
    Container container_;
};

我走的路正确吗?

  • 修复了一元构造函数。
  • 改进的代码。

Continuing List to priority queue

I'm implementing a improved priority_queue with random access.

template <class T, class Container = std::vector<T> >
class Heap {
public:
    Heap() {}

    Heap(const Container& container) {
        container_ = container;
        std::make_heap(container_.begin(), container_.end());
    }

    Heap<T, Container>& operator=(const Heap<T, Container>& heap) {
        if (this != &heap)
            container_ = heap.container_;

        return *this;
    }

    void push(const T& x) {
        container_.push_back(x);
        std::push_heap(container_.begin(), container_.end());
    }

    void pop() {
        std::pop_heap(container_.begin(), container_.end());
        container_.pop_back();
    }

    const T& top() {
        return container_.front();
    }

    const Container& getContainer() const {
        return container_;
    }

    T& operator[](size_t n) {
        return container_[n];
    }

    typename Container::const_iterator begin() const {
        return container_.begin();
    }

    typename Container::const_iterator end() const {
        return container_.end();
    }

    size_t size() const {
        return container_.size();
    }

    T& base() {
        return container_.back();
    }

    Container::iterator erase(Container::iterator position) {
        return container_.erase(position);
    }

private:
    Container container_;
};

Am I taking the right way?

  • Fixed the unary constructor.
  • Improved code.

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

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

发布评论

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

评论(2

夏九 2024-10-14 02:51:40

对我来说看起来不太好:

  • 一元构造函数应该通过 const 引用获取参数。
  • 赋值运算符不检查自赋值。
  • getContainer() 方法显示接口缺乏清晰度 - 为什么要简单地公开这样的实现细节?
  • 最重要的是:为什么需要“随机访问优先级队列”?

Doesn't look that great to me:

  • The unary constructor should take argument by const reference.
  • The assignment operator doesn't check for self-assignment.
  • The getContainer() method shows a lack of clarity in the interface - why would you simply expose the implementation detail like that?
  • Most importantly: why do you want a "random access priority queue"?
或十年 2024-10-14 02:51:40

您的 pop() 方法可能会违反堆顺序。使用 pop_heap() 而不是 pop_back()。我现在似乎找不到任何其他错误。

您可以通过推入随机整数并逐个 pop() 来轻松测试此类实现。您应该按排序顺序将它们取回。这称为堆排序。

建议:

  • 您可以为此类实现一个 const 迭代器,而不是返回对容器的引用。

  • 请注意,不应更改随机访问元素的键,因为这可能会破坏堆结构。如果您需要这样的功能,您应该实现一个change_key函数,它将安全地更改密钥并维护堆排序。

Your pop() method can violate the heap ordering. Use pop_heap() instead of pop_back(). I can't seem to find any other bug right now.

You can easily test such an implementation by pushing in a random integers and pop() them one by one. You should get them back in sorted order. This is known as heap sort.

Suggestions:

  • Instead of returning a ref to the container you could implement an const iterator to this class.

  • Note that you should not change the key of the randomly accessed element because it may destroy the heap structure. If you need such functionality you should implement a change_key function which would change the key safely and maintain the heap ordering.

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