In C++, std::vector::pop_back() removes the last element of a std::vector. 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 <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax
Call pop_back with empty brackets. It takes no arguments.
C++
v.pop_back();
Call
What it does
v.pop_back()
Removes the last element and makes the vector one element shorter.
A vector has no 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 <vector>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3};
v.pop_back();
for (int n : v) {
std::cout << n << ' ';
}
std::cout << "(size " << v.size() << ")\n";
return 0;
}
Output
1 2 (size 2)
How It Works
The vector 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 <vector>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3};
for (int i = 1; i <= 5; ++i) {
if (v.empty()) {
std::cout << "round " << i << ": nothing left to pop\n";
break;
}
v.pop_back();
std::cout << "round " << i << ": size is " << v.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 vector 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 vector.
Example 3 — Read with back(), Then Remove
pop_back() returns nothing, so keep the value before you remove it.
C++
#include <vector>
#include <iostream>
#include <string>
int main() {
std::vector<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 vector. 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 vector. The vector 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.