C++ .pop_back()

LukaPerkovic's avatar
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().

  • 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

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 elements
std::deque<int> numbers = {10, 20, 30, 40};
// Check if the deque contains elements
if (!numbers.empty()) {
// Display the last element from the deque before removal
std::cout << "Last element: " << numbers.back() << std::endl;
// Remove the last element from the deque
numbers.pop_back();
// Display the new last element from the deque after removal
std::cout << "New last element: " << numbers.back() << std::endl;
} else {
// Display if the deque is empty
std::cout << "Deque is empty" << std::endl;
}
return 0;
}

The above code generates the following output:

Last element: 40
New 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:

Code
Output
Loading...

All contributors

Contribute to 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