How to preallocate(reserve) a priority_queue?

前端 未结 4 956
-上瘾入骨i
-上瘾入骨i 2021-01-01 20:35

How can I preallocate a std::priority_queue with a container of type std::vector?

std::priority_queue

        
4条回答
  •  别那么骄傲
    2021-01-01 20:48

    Another solution might be to make your own class derived from std::priority_queue, such as:

    class MyPQueue : public std::priority_queue>
    {
    public:
        MyPQueue(size_t reserve_size)
        {
            this->c.reserve(reserve_size);
        }
    };
    

    then, in the code, create a MyPQueue object in this way:

    MyPQueue mpq(1024);
    

    which object can be upcasted back to the base class whenether needed.

    std::priority_queue>& pq = mpq;
    

提交回复
热议问题