How to make STL's priority_queue fixed-size

前端 未结 5 775
北海茫月
北海茫月 2020-12-17 18:41

I am creating a simple game and I use std::priority_queue for giving commands to squads (every squad has a priority_que

5条回答
  •  余生分开走
    2020-12-17 19:31

    Aryabhatta's answer of another question applies to this question.

    You use a max-heap.

    Say you have an N element heap (implemented as an array) which contains the N smallest elements seen so far.

    When an element comes in you check against the max (O(1) time), and reject if it is greater.

    Iteration mentioned by several earlier comments is unnecessary.

提交回复
热议问题