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.
Syntax
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
Reference
How FIFO Works
Think of a line at a ticket counter. This table shows what each end of a std::queue does:
Feature
Front
Back
Meaning
The oldest element, waiting the longest
The newest element, joined last
Read it with
front()
back()
Change it with
pop() removes it
push() and emplace() add a new one here
Leaves the queue
First
Last
Pop order for 5, 8, 2
5, 8, 2 (same as push order)
Typical use
Next customer, next print job
Newest request, newest message
Guide
queue vs priority_queue vs deque
All three are in the standard library, but each one is used for a different job:
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.