priority queue with limited space: looking for a good algorithm

后端 未结 8 1649
旧巷少年郎
旧巷少年郎 2020-12-06 02:07

This is not a homework.

I\'m using a small \"priority queue\" (implemented as array at the moment) for storing last N items with smallest value. This is a b

8条回答
  •  粉色の甜心
    2020-12-06 02:43

    Use std::priority_queue with the largest item at the head. For each new item, discard it if it is >= the head item, otherwise pop the head item and insert the new item.

    Side note: Standard containers will only grow if you make them grow. As long as you remove one item before inserting a new item (after it reaches its maximum size, of course), this won't happen.

提交回复
热议问题