C++ .pop_back()
Published Oct 13, 2025
Contribute to Docs
In C++, the .pop_back() method removes the element at the back of a deque.
If the deque is empty, calling .pop_back() results in undefined behavior (UB), meaning the program might crash or behave unpredictably. To prevent this, always check if the deque is not empty using .empty() before calling .pop_back().
Syntax
dequeName.pop_back();
Parameters:
.pop_back() method does not take any parameters.
Return value:
The .pop_back() method does not return a value.
Example
The example below showcases the use of the .pop_back() method, with a safety check using .empty() to avoid UB:
#include <iostream>#include <deque>int main() {// Initialize a deque of integers with 4 elementsstd::deque<int> numbers = {10, 20, 30, 40};// Check if the deque contains elementsif (!numbers.empty()) {// Display the last element from the deque before removalstd::cout << "Last element: " << numbers.back() << std::endl;// Remove the last element from the dequenumbers.pop_back();// Display the new last element from the deque after removalstd::cout << "New last element: " << numbers.back() << std::endl;} else {// Display if the deque is emptystd::cout << "Deque is empty" << std::endl;}return 0;}
The above code generates the following output:
Last element: 40New last element: 30
Codebyte Example
The following codebyte removes the last element from myDeque using the .pop_back() method and displays the updated size of the deque:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours