First of all: make_heap , push_heap , pop_heap and sort_heap are basically heap primitives, in “normal work” you usually use std::priority_queue , which is easier to use (although it has some other disgusting abilities, namely: impossibility direct access to the base container and the equivalent of sort_heap ).
push_heap and pop_heap are designed to work with the general range defined by the iterator, and, as usual, with STL algorithms (see, for example, various std::remove_if and co.), they cannot resize the base container (after all, everything that you passed to them are a few iterators).
For this reason, push_heap and pop_heap will not actually add an element to the container, but simply close the order inside the range to include a new element / delete the top element and return the corresponding heap property range back.
push_heap expects to receive the range in such a way that [first, last-1] is already a heap (that is, it has a heap property), and the element to be added to the heap is stored at the last position (ie last-1 ) After push_heap entire range [first, last] is now a bunch with the addition of a new element.
That's why in this example, push_back is executed first (so that the new item is ready to be processed using push_heap ), and then it calls push_heap .
Otherwise, pop_heap will start from the range [of the first, last], considered as a heap, then perform its actions, and in the end you will get the [first, last-1] heap, and in the last position (i.e. *(last-1) ) the highest element extracted from the correct heap range. Therefore, after pop_heap in the example pop_back , decreasing the size of the vector by 1 and thus discarding the extracted value, reducing the vector only to the range where it is a heap.
sort_heap instead because, although the heap uses a specific order to achieve its performance characteristics, it does not preserve range sorting; sort_heap destroys the heap (i.e., the range loses its heap properties) by sorting the elements (sorting uses heap properties, which could potentially give some performance boost compared to regular std::sort ).