Reputation: 862
I used heap-related operations to maintain a heap
structure.
For example:
std::vector<int> a = {1,2,56, 2};
std::make_heap(a.begin(), a.end());
// add
a.push_back(3);
std::push_heap(a.begin(), a.end());
// erase
std::pop_heap(a.begin(), a.end());
int v = a.back();
a.pop_back();
Recently, I find there is a structure named priority_queue
seems also implement a heap.
which has simpler function with push
pop
.
Is there any difference between these two? (performance, memory, and other things)
And which one you think is better?
can i use reserve
to reduce memory allocate time since the container is vector (for heap operation)?
Upvotes: 0
Views: 52
Reputation: 16803
See cppreference.com:
Working with a
priority_queue
is similar to managing a heap in some random access container, with the benefit of not being able to accidentally invalidate the heap.
And in case it isn't clear: your code manages a heap in a random access container.
Upvotes: 1