C++ std::pop_heap() Function

Definition
In C++, std::pop_heap() moves the largest element (the front) of a heap to the back of the range and fixes the rest so it is still a heap.
To really remove the element, call pop_back() on the vector afterwards.
Syntax
Add #include <algorithm>, then call std::pop_heap(). Start with these two lines.
std::pop_heap(v.begin(), v.end());
v.pop_back();
| Call | What it does |
|---|
std::pop_heap(v.begin(), v.end()); | Move the top element to v.back(). |
v.pop_back(); | Delete that element from the vector. |
Library declarations
template<class RandomIt>
void pop_heap(RandomIt first, RandomIt last);
template<class RandomIt, class Compare>
void pop_heap(RandomIt first, RandomIt last, Compare comp);
Parameters
| Parameter | Description |
|---|
| first, last | Required. A range that is already a heap. |
| comp | Optional. The comparison used to build the heap. |
Return Value
| Return Value | Description |
|---|
void | Nothing is returned. Read the removed value from v.back(). |
C++ Examples
Example 1 — Remove the largest
Take the biggest value out of a heap.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {9, 5, 7, 1};
std::make_heap(v.begin(), v.end());
std::pop_heap(v.begin(), v.end());
std::cout << "Removed: " << v.back() << '\n';
v.pop_back();
std::cout << "New largest: " << v.front() << '\n';
return 0;
}
Removed: 9
New largest: 7
How It Works
pop_heap() swaps the front with the last element and repairs the rest.- The old largest value, 9, is now at the back, and
pop_back() drops it. - The next largest value, 7, is at the front.
Example 2 — Drain a heap
Pop until the heap is empty to read values from large to small.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {4, 8, 2, 6};
std::make_heap(v.begin(), v.end());
while (!v.empty()) {
std::pop_heap(v.begin(), v.end());
std::cout << v.back() << ' ';
v.pop_back();
}
std::cout << '\n';
return 0;
}
How It Works
- Each loop round moves the largest remaining value to the back.
- We print it and then remove it.
- The values come out in descending order, which is a heap sort.
Example 3 — Min-heap pop
Use std::greater to take out the smallest value.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 3, 8};
std::make_heap(v.begin(), v.end(), std::greater<int>());
std::pop_heap(v.begin(), v.end(), std::greater<int>());
std::cout << "Smallest: " << v.back() << '\n';
v.pop_back();
std::cout << "Next smallest: " << v.front() << '\n';
return 0;
}
Smallest: 3
Next smallest: 5
How It Works
- The heap is built with
std::greater<int>(), so the smallest value is on top. - The same comparison must be passed to
pop_heap(). - 3 is removed first, and then 5 is on top.
Frequently Asked Questions
In C++, std::pop_heap() moves the top element of a heap to the end of the range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). The top element moves to the last position and the rest stays a heap.
No. It only moves it to the back. Call v.pop_back() to remove it.
Pass the same comparison, such as std::greater<int>(), to make_heap(), push_heap() and pop_heap().
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful