C++ std::deque push_back() Function

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

Definition

In C++, std::deque::push_back() adds a new element at the back of a std::deque. The size becomes one larger, and the new element becomes the last one.

It is fast, and it works just like push_back() on a std::vector. The big difference is that a deque also has push_front(). 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

Pass the value to add. There are two forms: copy and move.

C++
d.push_back(30);
names.push_back(std::string("Mia"));
CallWhat it does
d.push_back(30)Adds a copy of 30 after the current last element.
names.push_back(std::string("Mia"))Adds a temporary object. It is moved into the deque instead of being copied.

If the element needs several constructor arguments, emplace_back() can build it in place.

Library declarations
C++
void push_back(const T& value);
void push_back(T&& value);   // C++11

Parameters

ParameterDescription
valueRequired. The value to add. A normal value is copied, and a temporary is moved into the deque.

Return Value

Return ValueDescription
Nothingpush_back() returns void. Use back() if you want to see the new element.

C++ Examples

Example 1 — Basic push_back()

Add two numbers to the back of a small deque.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {10, 20};

    d.push_back(30);
    d.push_back(40);

    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The deque starts as 10 20.
  2. Each push_back() adds its value after the current last element.
  3. The values appear in the order they were added: 10 20 30 40.

Example 2 — Build a Sequence with push_back()

Each new number is the sum of the last two numbers.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {0, 1};

    for (int i = 0; i < 6; ++i) {
        d.push_back(d[d.size() - 1] + d[d.size() - 2]);
    }

    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The deque starts with the first two Fibonacci numbers, 0 and 1.
  2. In each round, d[d.size() - 1] is the last number and d[d.size() - 2] is the one before it. Their sum is added with push_back().
  3. After six rounds, the deque holds eight numbers. The loop is able to use [] because a deque supports fast access by index.

Example 3 — References Stay Valid After push_back()

A reference to an old element keeps working, even after many new elements are added.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {10, 20, 30};
    int& first = d[0];

    for (int i = 0; i < 100; ++i) {
        d.push_back(i);
    }

    std::cout << "first = " << first << '\n';
    std::cout << "size = " << d.size() << '\n';
    return 0;
}

How It Works

  1. int& first = d[0] keeps a reference to the first element.
  2. The loop adds 100 more elements with push_back(). A deque never moves the old elements when it grows at the back.
  3. So first still refers to the same element and prints 10. With a std::vector, the reference could become invalid after the vector grows.

Frequently Asked Questions

It adds a new element at the back of the deque. The size grows by one.
No. It returns void. Use back() to see the element you added, or use emplace_back() in C++17 to get a reference.
Yes. It takes constant time (amortized). The deque grows block by block, so old elements are not moved.
Iterators are invalidated, but references and pointers to existing elements stay valid. This makes a deque safer than a vector in some cases.
push_back() takes an existing object. emplace_back() takes constructor arguments and builds the object in place.

Next: push_front()

Add an element at the front with std::deque.

push_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.

12 people found this page helpful