-1

c++ boost has a big top heap implementation?how can i use it?or how can i use c++ boost implements a priority_queue?i have no idea now.i have used stl::priority_queue,but it do not meet my requirement. If I push 5 elements into a c++ priority_queue,then pop() elements,i want memory space of queue will be release

wwb1
  • 1
  • 1
  • What's a "big top heap"? I've never heard of such a thing. Anyway, boost's heap implementations: https://www.boost.org/doc/libs/1_79_0/doc/html/heap.html – Homer512 May 24 '22 at 08:11
  • `std::prioty_queue` uses `std::vector` to store element by default and that has a `shrink_to_fit` method – 463035818_is_not_an_ai May 24 '22 at 08:14

1 Answers1

1

Assuming that by "big top heap" you mean a max heap: std::priority_queue is a min heap by default, but you can make it into a max-heap by giving it a different comparator.

std::priority_queue itself isn't in charge of it memory allocations; that's handled by the underlying container. By default, that is std::vector, which doesn't free any memory when it shrinks. But you can create your own wrapper around std::vector which calls shrink_to_fit at appropriate times.

Note that there is usually no reason to do this. The memory is freed when the heap is destroyed. But if it's long-lived, usually small, but occasionally big enough to cause memory pressure on the system, then this technique might be needed.

Thomas
  • 174,939
  • 50
  • 355
  • 478