C++ std::queue::push() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

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
CallWhat 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

ParameterDescription
valueRequired. 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 ValueDescription
Nothingpush() 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.

C++
#include <iostream>
#include <queue>

int main() {
    std::queue<int> q;
    q.push(10);
    q.push(40);
    q.push(20);

    std::cout << "Size: " << q.size() << '\n';
    std::cout << "Order out:";
    while (!q.empty()) {
        std::cout << ' ' << q.front();
        q.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Each push() adds one number behind the earlier ones. After three calls size() reports 3.
  2. 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.
  3. 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;
}

How It Works

  1. push("pear") builds a temporary std::string from the text. The queue then moves that temporary into place.
  2. 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.
  3. 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.

C++
#include <iostream>
#include <queue>
#include <string>

struct Task {
    int priority;
    std::string name;
};

int main() {
    std::queue<Task> tasks;
    tasks.push({2, "Write notes"});
    tasks.push({9, "Fix bug"});
    tasks.push({5, "Review code"});

    while (!tasks.empty()) {
        std::cout << tasks.front().name << " (" << tasks.front().priority << ")\n";
        tasks.pop();
    }
    return 0;
}

How It Works

  1. Task is a plain struct with no operator<. A std::queue never compares elements, so none is needed.
  2. Each push({...}) builds a Task from the braces and adds it at the back. The tasks sit in the order 2, 9, 5.
  3. 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().

Next: push_range()

Add every element of a range at the back (C++23) with std::queue.

push_range() 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