Hi all. I'm studying std::make_heap, and I need an explanation.
From what I have understood so far, if I have a vector v, I can organize it into a heap with:
std::make_heap(v.begin(), v.end()); |
If I want to insert a new element, I first push it into the vector, and then I update the heap:
v.push_back(100);
std::push_heap (v.begin(),v.end()); |
If I want to pop the max value, first I call pop_heap to rearrange the heap without the first element, and then I take it away from the back of vector
std::pop_heap (v.begin(),v.end());
v.pop_back();
|
So pop_heap() puts the element to be “popped” in the last position of the vector, and rearranges the sub-range v.begin(),v.end()-1 , am I right?
If I want the min value, first I sort the heap:
std::sort_heap (v.begin(),v.end()); |
and then - as long as I don't delete or insert any new value - I get the min from
.
Is there a way to build a C++ heap "that allows for fast retrieval of the element with the
lowest value at any moment", namely, a heap arranged from min to max?