'i want a C++ priority-queue implement, and when the element is popped, the corresponding queue space is released? [closed]

Which C++ third-party library implements the queue, and when the element is popped, the corresponding queue space is released?

c++ stl library support queue.but when i pop() some elements,queue use memory does not be samll.So i want to find a third-party library which can solve this problem.



Solution 1:[1]

The C++ standard library has a class that suits your needs: It's std::list. Just use its push_front and pop_back member functions. The memory of any element you pop will immediately be freed.

Note that this is only worth the trouble if your elements are large. Otherwise, the allocation overhead of the linked list nodes will outweigh the advantage of freeing nodes immediately.

Also keep in mind that your malloc implementation won't give the memory back to the operating system immediately. (Or at all, really. Looking at you, glibc.) So you may not see the program's memory usage decrease immediately, anyway. However, your program can immediately re-use the memory of any freed nodes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1