c++ - C++中 "bounded priority queue"的自由实现

标签 c++ stl priority-queue

我正在寻找用 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

有谁知道这样的事情的良好实现?有什么经验吗?

最佳答案

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

关于c++ - C++中 "bounded priority queue"的自由实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5329312/

相关文章:

c++ - 对容器中所有元素的成员函数结果求和的最佳方法是什么?

Java优先级队列

java - 打印优先队列的内容

c++ - 为什么同样的代码通过gcc可以编译成功,而g++却不行?

c++ - 如何在 Windows XP 上获取连接的显示器类型?

C++ cout 分离

c++ - 为什么仅当我们返回 *this 时才调用 Copy 构造函数?

c++ - std::mutiset 与 std::vector 读取排序字符串并将其写入文件

c++ - 在 c++0x 之前的版本中使用 map::at()

c++ - 具有自定义类和 lambda 表达式的 C++ 中的优先级队列