How to preallocate(reserve) a priority_queue?

前端 未结 4 906
-上瘾入骨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:54

    Yes, there's a constructor for that. It's slightly tedious that you also have to specify a comparator:

    std::vector container;
    container.reserve(1024);
    std::priority_queue> pq (
        std::less(), std::move(container));
    

    You can also use evil shenanigans to access the protected member, but I wouldn't recommend it.

提交回复
热议问题