简体   繁体   English

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

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

I'm looking for a free software implementation of the bounded priority queue abstraction in C++. 我正在寻找C ++中有界优先级队列抽象的自由软件实现。 Basically, I need a data structure that will behave just like std::priority_queue but will at all times hold the "best" n elements at most. 基本上,我需要一个与std::priority_queue类似的数据结构,但最多只能保存“最佳” n个元素。

Example: 例:

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

Does anyone know of a good implementation of such thing? 有谁知道这样的事情的良好实施? Any experience with it? 有经验吗?

I think that the algorithm discussed in this thread is probably what you are looking for. 我认为这个线程中讨论的算法可能就是你要找的东西。 If you want to get a head start, you might want to consider building upon Boost's implementation d_ary_heap_indirect which is part of Boost.Graph (in d_ary_heap.hpp ). 如果你想要d_ary_heap_indirect ,你可能需要考虑构建Boost的实现d_ary_heap_indirect ,它是d_ary_heap_indirect的一部分(在d_ary_heap.hpp )。 If you do a good job with it, you might submit it to Boost. 如果你做得很好,可以将它提交给Boost。 It could make a nice little addition, because such an implementation certainly has many uses. 它可以做一个很好的小补充,因为这样的实现肯定有很多用途。

Why not use a std::vector with a functor/comparison function and std::sort() it into the correct order? 为什么不将std :: vector与functor / comparison函数和std :: sort()一起使用到正确的顺序? The code is probably pretty trivial 代码可能非常简单

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

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