Home C++ Deques Functions push_front() C++ std::deque push_front() Function Definition In C++, std::deque::push_front() adds a new element at the front of a std::deque. The size becomes one larger, and the new element becomes the first one.
This is the main thing a deque can do that a std::vector cannot do quickly. A vector has no push_front(), because every element would have to move. 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_front(10);
words.push_front(std::string("hello")); Call What it does d.push_front(10)Adds a copy of 10 before the current first element. words.push_front(std::string("hello"))Adds a temporary object. It is moved into the deque instead of being copied.
The matching back version is push_back(). For constructor arguments, use emplace_front().
Library declarations void push_front(const T& value);
void push_front(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_front() returns void. Use front() if you want to see the new element.
Important
Each new push_front() goes before all older elements, so pushing 1, 2, 3 gives the order 3 2 1. Like push_back(), it invalidates iterators but keeps references to old elements valid. Every index also shifts up by one.
C++ Examples 1. Basic push_front 2. Both ends 3. Newest first Example 1 — Basic push_front() Add a word in front of an existing word.
#include <deque>
#include <iostream>
#include <string>
int main() {
std::deque<std::string> words = {"world"};
words.push_front("hello");
for (const std::string& w : words) {
std::cout << w << ' ';
}
std::cout << '\n';
return 0;
} How It Works The deque starts with one word, "world". push_front("hello") places "hello" before it, so it becomes the first element.The loop prints from front to back: hello world. Example 2 — push_front() and push_back() Together Grow the deque from both ends and watch the order.
#include <deque>
#include <iostream>
int main() {
std::deque<int> d;
d.push_back(2);
d.push_front(1);
d.push_back(3);
d.push_front(0);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
} How It Works push_back(2) adds the first element, so the deque is 2.push_front(1) gives 1 2, push_back(3) gives 1 2 3, and push_front(0) gives 0 1 2 3.Both ends are fast. A vector can only add quickly at the back. Example 3 — Keep the Newest Item First Use push_front() so the most recent message is always at the front.
#include <deque>
#include <iostream>
#include <string>
int main() {
std::deque<std::string> log;
log.push_front("boot");
log.push_front("login");
log.push_front("logout");
for (const std::string& line : log) {
std::cout << line << '\n';
}
return 0;
} How It Works The messages are pushed in time order: boot, then login, then logout. Each push_front() places the new message before all older ones. Reading from the front gives the newest message first. With push_back() the order would be the opposite. Frequently Asked Questions What does std::deque::push_front() do? It adds a new element at the front of the deque. The new element becomes the first one, and the size grows by one.
Does std::vector have push_front()? No. Adding at the front of a vector would move every element, which is slow. Use a deque when you need fast adds at the front.
Is push_front() fast? Yes. It takes constant time (amortized), because the deque keeps free space at the front.
Why is the order reversed after several push_front() calls? Each new element goes before all older ones. The last element you push is the first element in the deque.
What is the difference between push_front() and insert(begin(), x)? Both add at the front. push_front() is shorter and clearer. insert(begin(), x) also returns an iterator.
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.
13 people found this page helpful
Helpful Share Copy link Suggestion