In C++, push() adds one new element at the back of a std::queue. This is the enqueue step: a new person joins the end of the line.
The queue never sorts. If you push 10, then 50, then 20, they will leave in the same order: 10, 50, 20. The first element pushed is the first one that front() shows and that pop() removes.
This function is a member function of std::queue<T>, declared in the <queue> header. It has been available since the first C++ standard, so every compiler supports it. You call it with a dot, for example q.push(...).
Syntax
Call push() with the value you want to add. Two overloads exist: one copies the value, the other moves it.
C++
std::queue<int> q;
q.push(10);
q.push(50); // goes behind 10
q.push(20);
int next = q.front(); // 10, the first one pushed
Call
What it does
q.push(value)
Copies value and adds it at the back of the queue.
q.push(std::move(value))
Moves value into the queue at the back. It is faster for big objects such as long strings.
push() returns nothing, so you cannot use the result. Each call usually takes constant time. If you want to build the object right inside the queue, see emplace(). To add many values in one call with C++23, see push_range().
Library declarations
C++
void push(const value_type& value);
void push(value_type&& value); // C++11
// queue<T, Container> from <queue>
// It calls c.push_back(value) on the underlying container.
Parameters
Parameter
Description
value
Required. The element to add. It must have the type T of the queue, or be convertible to it. It is placed at the back.
Return Value
Return Value
Description
Nothing
push() returns void. The queue is changed in place, and its size grows by one.
C++ Examples
Example 1 — Push Numbers and Keep the Order
Push three numbers and then read them back. They come out in the same order as they went in.
Each push() adds one number behind the earlier ones. After three calls size() reports 3.
The numbers go in as 10, 40, 20 and come out as 10, 40, 20. The larger value 40 does not jump ahead, because a queue does not sort.
The loop reads front() and removes it with pop(), until empty() says nothing is left.
Example 2 — Copy a String or Move It
Pushing a named variable makes a copy. Wrapping it in std::move hands the text over to the queue without copying.
C++
#include <iostream>
#include <queue>
#include <string>
#include <utility>
int main() {
std::queue<std::string> words;
std::string first = "apple";
std::string second = "banana";
words.push("pear");
words.push(first);
words.push(std::move(second));
std::cout << "Original after copy: " << first << '\n';
std::cout << "Order out:";
while (!words.empty()) {
std::cout << ' ' << words.front();
words.pop();
}
std::cout << '\n';
return 0;
}
Output
Original after copy: apple
Order out: pear apple banana
How It Works
push("pear") builds a temporary std::string from the text. The queue then moves that temporary into place.
push(first) uses the copy overload, so first still holds "apple" afterwards. push(std::move(second)) uses the move overload, so second should not be used again.
The three strings leave in push order: pear, apple, banana. Copy or move changes how the value gets in, not where it sits.
Example 3 — Push Your Own Type (No Comparison Needed)
A queue does not compare its elements, so your struct needs no operator<. The priority number is stored, but it does not change the order.
Task is a plain struct with no operator<. A std::queue never compares elements, so none is needed.
Each push({...}) builds a Task from the braces and adds it at the back. The tasks sit in the order 2, 9, 5.
The loop prints them in that same arrival order, even though Fix bug has the highest number. To serve the biggest priority first, use std::priority_queue instead.
Frequently Asked Questions
It adds one element at the back of the queue. The new element will leave after every element that is already in the queue.
No. A queue keeps elements in arrival order. If you need the biggest or most important element first, use std::priority_queue instead.
Nothing. push() returns void. If you need a reference to the new element, use emplace() in C++17 or later, or read q.back() right after the push.
push() takes a ready-made object and copies or moves it in. emplace() takes the constructor arguments and builds the object directly inside the queue, which can avoid an extra copy.
No. A queue only adds at the back. If you need to add at both ends, use std::deque with push_front() and push_back().