C++ queue

Beginner
8 min read
Updated: Nov 2026
3 examples
<queue>

Definition and Usage

In C++, std::queue<T> is a container from the <queue> header that works like a waiting line. New elements join at the back, and elements leave from the front. The one that came first always leaves first. This rule is called FIFO (first in, first out).

The daily pattern is simple: push() to add at the back, front() to read the next element, and pop() to remove it. Use empty() before you read or remove.

This page teaches what a queue is. The queue Functions index lists every function guide in this series.

You can only see the two ends.

A queue has no [], no at(), and no begin() or end(). You cannot loop over all elements with iterators. Calling front(), back(), or pop() on an empty queue is undefined behavior, so check empty() first. Also remember that pop() removes the front element but does not return it.

C++ Syntax

Common ways to create and use a std::queue:

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

std::queue<int> q;                          // empty queue, uses deque inside
std::queue<int, std::list<int>> ql;          // use a list inside instead

q.push(10);                                 // join at the back
q.push(40);
q.emplace(20);                              // build in place (C++11)

int oldest = q.front();                     // 10, next to leave
int newest = q.back();                      // 20, last to leave
q.pop();                                    // removes 10, returns nothing
std::size_t n = q.size();                   // 2
bool none = q.empty();                      // false

std::deque<int> d = {3, 1, 2};
std::queue<int> fromDeque(d);                // copy the elements into a queue

How FIFO Works

Think of a line at a ticket counter. This table shows what each end of a std::queue does:

FeatureFrontBack
MeaningThe oldest element, waiting the longestThe newest element, joined last
Read it withfront()back()
Change it withpop() removes itpush() and emplace() add a new one here
Leaves the queueFirstLast
Pop order for 5, 8, 25, 8, 2 (same as push order)
Typical useNext customer, next print jobNewest request, newest message

queue vs priority_queue vs deque

All three are in the standard library, but each one is used for a different job:

Featurestd::queuestd::priority_queuestd::deque
Order outOrder of arrival (FIFO)Highest priority firstYour choice, from either end
Read itemsfront() and back()top() onlyfront(), back(), [], iterators
Add itemspush() at the backpush(), order is decided insidepush_back() and push_front()
Remove itemspop() from the frontpop() the toppop_back() and pop_front()
Use it forWaiting lines, breadth-first searchTask scheduling, shortest pathFast access at both ends

Pick the function by the job you want to do:

FunctionUse it when you want to…
push() / emplace()Add one element at the back. Use emplace() to build a pair or struct right inside the queue
push_range()Add a whole vector or array at once (needs C++23)
front() / back()Read the oldest or the newest element without removing it
pop()Remove the front element. It returns nothing, so read with front() first
empty() / size()Check that the queue has elements before front() or pop(), or count them
swap()Exchange two queues of the same type, or empty a queue

C++ Examples

Three short programs that show how a std::queue works.

Example 1 — Push, Front and Pop

A first program: three people join a line, you read both ends, then serve everyone in order.

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

int main() {
    std::queue<std::string> line;
    line.push("Asha");
    line.push("Ben");
    line.push("Chen");

    std::cout << "size: " << line.size() << '\n';
    std::cout << "front: " << line.front() << '\n';
    std::cout << "back: " << line.back() << '\n';

    std::cout << "serving:";
    while (!line.empty()) {
        std::cout << ' ' << line.front();
        line.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Three push() calls add Asha, Ben, and Chen at the back, one after the other.
  2. front() shows Asha, who has waited the longest. back() shows Chen, who joined last. Both only read.
  3. The loop reads with front() and removes with pop() until empty() is true. People leave in the same order they joined: first in, first out.

Example 2 — A Line That Keeps Changing

In real programs, new items arrive while old ones are still being served. A queue handles this naturally.

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

int main() {
    std::queue<int> tickets;
    tickets.push(101);
    tickets.push(102);

    std::cout << "serve " << tickets.front() << '\n';
    tickets.pop();

    tickets.push(103);

    std::cout << "serve " << tickets.front() << '\n';
    tickets.pop();

    std::cout << "waiting: " << tickets.size()
              << ", next: " << tickets.front() << '\n';
    return 0;
}

How It Works

  1. The queue starts as 101, 102. The first front() gives 101, and pop() removes it, so only 102 is left.
  2. Ticket 103 arrives later. push() puts it at the back, behind 102, even though 103 is a bigger number.
  3. 102 is served next because it has waited longer. Then one ticket remains, so size() is 1 and front() is 103.

Example 3 — queue vs priority_queue

Both live in <queue>. A normal queue serves items in arrival order. A priority queue serves the biggest value first.

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

int main() {
    std::queue<int> line;
    std::priority_queue<int> pq;

    int values[] = {3, 9, 1};
    for (int v : values) {
        line.push(v);
        pq.push(v);
    }

    std::cout << "queue order:          ";
    while (!line.empty()) {
        std::cout << line.front() << ' ';
        line.pop();
    }
    std::cout << '\n';

    std::cout << "priority_queue order: ";
    while (!pq.empty()) {
        std::cout << pq.top() << ' ';
        pq.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The same three numbers 3, 9, 1 go into both containers with push(). Only the container type is different.
  2. std::queue never sorts. It reads with front() and gives the numbers back in the order they arrived: 3 9 1.
  3. std::priority_queue reads with top() and ignores the arrival order. The biggest number comes out first: 9 3 1.

Next: C++ queue Functions Index

After you know what a queue is for, open the function index for a full tutorial on each function.

Start withWhy
push()Add elements at the back, the first thing every queue needs
front()Read the next element to leave without removing it
pop()Remove the front element (remember: it returns nothing)
emplace()Build pairs and structs right inside the queue

Full list: C++ std::queue Functions index (/cpp/queue/function).

Frequently Asked Questions

std::queue is a container adaptor from the <queue> header. It works like a waiting line: you add elements at the back with push() and remove them from the front with pop(). The first element added is the first one removed (FIFO).
FIFO means first in, first out. The element that has waited the longest leaves first, just like people in a line at a shop. You read that element with front() and remove it with pop().
std::queue serves elements in the order they arrived and reads with front(). std::priority_queue serves the highest-priority element first and reads with top(), no matter when it was added.
No. A queue has no iterators and no operator[]. You can only see front() and back(). To visit every element, loop with while (!q.empty()) and call front() then pop().
Open the C++ std::queue Functions index. It lists all 9 member functions with a short description, the C++ version needed, and a full tutorial for each.

Browse every queue function

Open the functions index for categorized guides with examples and FAQs.

Queue Functions Index →

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.

8 people found this page helpful