繁体   English   中英

在C ++中自由实现“有界优先级队列”

[英]Free implementation of “bounded priority queue” in C++

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

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

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

为什么不将std :: vector与functor / comparison函数和std :: sort()一起使用到正确的顺序? 代码可能非常简单

暂无
暂无

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

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