(C++17) | ||||
Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
Associative | ||||
Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Adaptors | ||||
queue | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
Views | ||||
(C++20) | ||||
(C++23) | ||||
Tables | ||||
Iterator invalidation | ||||
Member function table | ||||
Non-member function table |
std::queue
Member functions | ||||
Element access | ||||
Capacity | ||||
Modifiers | ||||
(C++23) | ||||
(C++11) | ||||
(C++11) | ||||
Non-member functions | ||||
(C++11) | ||||
Helper classes | ||||
(C++11) | ||||
(C++23) | ||||
Deduction guides(C++17) |
Defined in header <queue> | ||
template< class T, | ||
Thestd::queue
class template is acontainer adaptor that gives the functionality of aqueue - specifically, a FIFO (first-in, first-out) data structure.
The class template acts as a wrapper to the underlying container - only a specific set of functions is provided. The queue pushes the elements on the back of the underlying container and pops them from the front.
All member functions ofstd::queue areconstexpr: it is possible to create and usestd::queue objects in the evaluation of a constant expression.However, | (since C++26) |
Contents |
T | - | The type of the stored elements. The program is ill-formed ifT is not the same type asContainer::value_type . |
Container | - | The type of the underlying container to use to store the elements. The container must satisfy the requirements ofSequenceContainer. Additionally, it must provide the following functions with theusual semantics:
The standard containersstd::deque andstd::list satisfy these requirements. |
Member type | Definition |
container_type | Container [edit] |
value_type | Container::value_type [edit] |
size_type | Container::size_type[edit] |
reference | Container::reference [edit] |
const_reference | Container::const_reference [edit] |
Member name | Definition |
Container c | the underlying container (protected member object)[edit] |
constructs thequeue (public member function)[edit] | |
destructs thequeue (public member function)[edit] | |
assigns values to the container adaptor (public member function)[edit] | |
Element access | |
access the first element (public member function)[edit] | |
access the last element (public member function)[edit] | |
Capacity | |
checks whether the container adaptor is empty (public member function)[edit] | |
returns the number of elements (public member function)[edit] | |
Modifiers | |
inserts element at the end (public member function)[edit] | |
(C++23) | inserts a range of elements at the end (public member function)[edit] |
(C++11) | constructs element in-place at the end (public member function)[edit] |
removes the first element (public member function)[edit] | |
(C++11) | swaps the contents (public member function)[edit] |
lexicographically compares the values of twoqueue s(function template)[edit] | |
(C++11) | specializes thestd::swap algorithm (function template)[edit] |
specializes thestd::uses_allocator type trait (class template specialization)[edit] | |
(C++23) | formatting support forstd::queue (class template specialization)[edit] |
Deduction guides | (since C++17) |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_containers_ranges | 202202L | (C++23) | Ranges construction and insertion for containers |
__cpp_lib_constexpr_queue | 202502L | (C++26) | constexprstd::queue |
#include <cassert>#include <iostream>#include <queue> int main(){ std::queue<int> q; q.push(0);// back pushes 0 q.push(1);// q = 0 1 q.push(2);// q = 0 1 2 q.push(3);// q = 0 1 2 3 assert(q.front()==0);assert(q.back()==3);assert(q.size()==4); q.pop();// removes the front element, 0assert(q.size()==3); // Print and remove all elements. Note that std::queue does not// support begin()/end(), so a range-for-loop cannot be used.std::cout<<"q: ";for(;!q.empty(); q.pop())std::cout<< q.front()<<' ';std::cout<<'\n';assert(q.size()==0);}
Output:
q: 1 2 3
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 307 | C++98 | std::queue did not support containers using proxyreference types[1] in place of ( const )value_type& | supported |
LWG 2566 | C++98 | Missing the requirement forContainer::value_type | ill-formed ifT is not the same type asContainer::value_type |
pop_front()
. The resolution of this DRstd::queue
adapts a container to provide priority queue (class template)[edit] | |
double-ended queue (class template)[edit] | |
doubly-linked list (class template)[edit] |