C++ Reference ยท Containers
C++ std::queue class
Reference for std::queue: the first-in first-out adapter with push, pop, front, back, emplace and swap, its underlying container, and breadth-first search.
std::queue<T> is a container adapter with a first-in, first-out (FIFO) interface: elements are added at the back with push and removed from the front with pop, and you can look at both ends with front() and back(). Like std::stack, it wraps another container (by default std::deque<T>) and hides everything else, so you cannot iterate or index it.
Queues model waiting lines: task schedulers, message buffers, breadth-first search, level-order tree traversal. A queue is not thread-safe; if several threads share one, protect it with a mutex (see the threads lesson).
#include <queue> std::queue<int> q; q.push(1); q.push(2); q.front(); // 1 q.pop(); // removes 1, returns nothing
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template <class T, class Container = std::deque<T>> class queue; void push(const T& v); void push(T&& v); template <class... A> decltype(auto) emplace(A&&... args); void pop(); T& front(); T& back(); // const overloads exist bool empty() const; size_type size() const; void swap(queue& other) noexcept;
| Name | Description |
|---|---|
T |
Element type. |
Container |
Underlying sequence; needs front(), back(), push_back(), pop_front(). deque and list work; vector does not (no pop_front). |
v |
Value to add at the back. |
Returns: front() and back() return references; pop() returns nothing. emplace returns a reference to the new element since C++17.
Errors / undefined behavior: front(), back() and pop() on an empty queue are undefined; check empty() first.
Usage notes
pop()returnsvoid: readfront()first, copy or move it out if needed, then callpop(). A reference taken fromfront()dangles afterpop().- Complexity:
push,pop,front,back,sizeare all O(1). - No iteration, no
clear(), no random access. If you need to inspect all elements, use astd::dequeorstd::listdirectly. vectorcannot be the container, because removing from the front of a vector is O(n) and the memberpop_frontis missing.- BFS pattern: push the start, then loop: take
front(),pop(), push unvisited neighbors. Mark nodes visited when pushing, not when popping, to avoid duplicates in the queue. - Ordering by priority instead of arrival time needs
std::priority_queue. - Comparison:
==,<and the rest compare the underlying containers. - Range construction (
std::queue<int> q(std::from_range, r)) is C++23.