C++ std::deque pop_back() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::deque

Definition

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();
CallWhat 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

ParameterDescription
Nonepop_back() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
Nothingpop_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;
}

How It Works

  1. The deque starts as 1 2 3, with 3 at the back.
  2. pop_back() removes that last element. No other element moves.
  3. 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;
}

How It Works

  1. The loop wants to run five times, but the deque has only three elements.
  2. In rounds 1 to 3, empty() is false, so pop_back() runs and the size goes down to 0.
  3. 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;
}

How It Works

  1. history.back() reads "save", and the copy is stored in last.
  2. pop_back() then deletes the element inside the deque. The copy in last is not affected.
  3. 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.

Next: pop_front()

Remove the first element with std::deque.

pop_front() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful