【问题标题】:Free implementation of "bounded priority queue" in C++C++中“有界优先队列”的免费实现
【发布时间】:2011-03-16 17:33:35
【问题描述】:

我正在寻找 C++ 中 有界优先级队列 抽象的免费软件实现。基本上,我需要一个行为类似于 std::priority_queue 的数据结构,但在任何时候都最多保存“最佳”n 个元素。

例子:

std::vector<int> items; // many many input items
bounded_priority_queue<int> smallest_items(5);
for(vector<int>::const_iterator it=items.begin(); it!=items.end(); it++) {
  smallest_items.push(*it);
}
// now smallest_items holds the 5 smallest integers from the input vector

有谁知道这种事情的良好实施?有经验吗?

【问题讨论】:

标签: c++ stl priority-queue


【解决方案1】:

我认为this thread 中讨论的算法可能是您正在寻找的。如果您想抢占先机,您可能需要考虑构建 Boost 的实现 d_ary_heap_indirect,它是 Boost.Graph 的一部分(在 d_ary_heap.hpp 中)。如果你做得很好,你可以将它提交给 Boost。它可以是一个不错的小补充,因为这样的实现肯定有很多用途。

【讨论】:

    【解决方案2】:

    为什么不使用带有仿函数/比较函数的 std::vector 和 std::sort() 它到正确的顺序? 代码可能很简单

    【讨论】:

    • 在某些情况下,它的性能可能比堆差得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多