簡體   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