In C++, std::deque::pop_back() removes the last element of a std::deque. The size becomes one smaller.
It is fast, because nothing else has to move. It does not return the removed value, so read it with back() first if you need it. Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax
Call pop_back with empty brackets. It takes no arguments.
C++
d.pop_back();
Call
What it does
d.pop_back()
Removes the last element and makes the deque one element shorter.
The front version is pop_front(). To add at the back, use push_back().
Library declarations
C++
void pop_back();
Parameters
Parameter
Description
None
pop_back() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Nothing
pop_back() returns void. The removed value is destroyed.
C++ Examples
Example 1 — Basic pop_back()
Remove the last number and print what is left.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3};
d.pop_back();
for (int n : d) {
std::cout << n << ' ';
}
std::cout << "(size " << d.size() << ")\n";
return 0;
}
Output
1 2 (size 2)
How It Works
The deque starts as 1 2 3, with 3 at the back.
pop_back() removes that last element. No other element moves.
The loop prints 1 2, and size() reports 2.
Example 2 — Pop Safely with empty()
Try to pop more times than there are elements, but check first.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3};
for (int i = 1; i <= 5; ++i) {
if (d.empty()) {
std::cout << "round " << i << ": nothing left to pop\n";
break;
}
d.pop_back();
std::cout << "round " << i << ": size is " << d.size() << '\n';
}
return 0;
}
Output
round 1: size is 2
round 2: size is 1
round 3: size is 0
round 4: nothing left to pop
How It Works
The loop wants to run five times, but the deque has only three elements.
In rounds 1 to 3, empty() is false, so pop_back() runs and the size goes down to 0.
In round 4, empty() is true. The code prints a message and stops with break, so pop_back() is never called on an empty deque.
Example 3 — Read with back(), Then Remove
pop_back() returns nothing, so keep the value before you remove it.
C++
#include <deque>
#include <iostream>
#include <string>
int main() {
std::deque<std::string> history = {"open", "edit", "save"};
std::string last = history.back();
history.pop_back();
std::cout << "removed: " << last << '\n';
std::cout << "now last: " << history.back() << '\n';
return 0;
}
Output
removed: save
now last: edit
How It Works
history.back() reads "save", and the copy is stored in last.
pop_back() then deletes the element inside the deque. The copy in last is not affected.
After the removal, "edit" is the new last element. Undo features often work like this.
Frequently Asked Questions
It removes the last element of the deque. The deque becomes one element smaller.
No. It returns void. Read the value with back() before you call pop_back().
It is undefined behavior. The program may crash or act strangely. Check empty() first.
Yes. It takes constant time, because no other element has to move.
pop_back() only removes the last element. erase() can remove any element or range, but it needs iterators and can be slower in the middle.