Note, this article is not finished! You can help by editing this doc page.
Overview
- Simplified
- Detailed
template< class T, /* ... */ >
class queue;
template<
class T,
class Container = std::deque<T>
>
class queue;
Queue is a container adapter - it adapts a container by providing a new interface to it.
The new interface is a FIFO (First In First Out) data structure. This means that the first element to be pushed is the first one to be accessed (like in a shop queue - first person to get in is the first person to be served).
Technical definition of a queue
std::queue
| Defined in | queue |
Template parameters
| pub | T | The type of the stored elements.
danger The behaviour is undefined if |
| pub | Container | The type of the underlying container used to store the elements. By default The container must satisfy SequenceContainer
The standard containers |
Type names
| pub | container_type | Container |
| pub | value_type | Container::value_type |
| pub | size_type | Container::size_type |
| pub | reference | Container::reference |
| pub | const_reference | Container::const_reference |
Member functions
| pub | (constructors) | Constructs a |
| pub | (destructor) | Destructs a |
| pub | operator= | Assigns one queue to another. |
Element access
| pub | front | Accesses the element at the top (first pushed). |
| pub | back | Accesses the element at the back (last pushed). |
Capacity
| pub | empty | Returns true if the queue is empty, otherwise false. |
| pub | size | Returns the number of elements in the queue. |
Modifiers
| pub | push | Inserts a new element at the end. |
| pub | emplace (since C++11) | Constructs a new element in-place at the end. |
| pub | pop | Removes the first pushed element (the one that would've been returned by queue.front()). |
| pub | swap (since C++11) | Swaps two queues. |
Member objects
| prot | Container C | The underlying container. By default std::deque<T>. |
Non-member functions
| pub | operator== operator!= operator< operator> operator<= operator>= operator<=> (C++20) | Lexicographically compares values in a queue. |
| pub | std::swap (std::queue) | An overload for the std::swap algorithm. |
Helper classes
| pub | std::uses_allocator (std::queue) | Specializes the std::uses_allocator type trait. |
Deduction guides (since C++17)
Click to expand
Examples
Basic manipulation
#include <iostream>
#include <queue>
int main()
{
std::priority_queue<int> q;
queue.push(1);
queue.push(2);
queue.push(3);
while (!queue.empty()) {
std::cout << queue.top() << '\n';
queue.pop();
}
}
1
2
3
Hover to see the original license.