Home C++ Deques Functions push_back() C++ std::deque push_back() Function 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.
d.push_back(30);
names.push_back(std::string("Mia")); Call What 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 void push_back(const T& value);
void push_back(T&& value); // C++11 Parameters Parameter Description valueRequired. The value to add. A normal value is copied, and a temporary is moved into the deque.
Return Value Return Value Description Nothing push_back() returns void. Use back() if you want to see the new element.
C++ Examples 1. Basic push_back 2. Build a sequence 3. References stay valid Example 1 — Basic push_back() Add two numbers to the back of a small deque.
#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 The deque starts as 10 20. Each push_back() adds its value after the current last element. 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.
#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 The deque starts with the first two Fibonacci numbers, 0 and 1. 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(). 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.
#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 int& first = d[0] keeps a reference to the first element.The loop adds 100 more elements with push_back(). A deque never moves the old elements when it grows at the back. 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 What does std::deque::push_back() do? It adds a new element at the back of the deque. The size grows by one.
Does push_back() return anything? No. It returns void. Use back() to see the element you added, or use emplace_back() in C++17 to get a reference.
Is push_back() fast on a deque? Yes. It takes constant time (amortized). The deque grows block by block, so old elements are not moved.
Are iterators and references still valid after push_back()? Iterators are invalidated, but references and pointers to existing elements stay valid. This makes a deque safer than a vector in some cases.
What is the difference between push_back() and emplace_back()? push_back() takes an existing object. emplace_back() takes constructor arguments and builds the object in place.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion