Przejdź do głównej zawartości
uwaga

Note, this article is not finished! You can help by editing this doc page.

Overview

template< class 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

The std::queue class is a container adapter that gives the programmer the functionality of a queue - 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.

std::queue

Defined inqueue

Template parameters

pubT

The type of the stored elements.

uwaga

The behaviour is undefined if T is not Container::value_type.

pubContainer

The type of the underlying container used to store the elements. By default std::deque<T>.

The container must satisfy SequenceContainer
Additionally, it must provide the following functions with the usual semantics:

  • back()
  • front()
  • push_back()
  • pop_front()

The standard containers std::deque and std::list satisfy these requirements.

Type names

pubcontainer_typeContainer
pubvalue_typeContainer::value_type
pubsize_typeContainer::size_type
pubreferenceContainer::reference
pubconst_referenceContainer::const_reference

Member functions

pub(constructors)

Constructs a queue.

pub(destructor)

Destructs a queue.

puboperator=Assigns one queue to another.

Element access

pubfrontAccesses the element at the top (first pushed).
pubbackAccesses the element at the back (last pushed).

Capacity

pubemptyReturns true if the queue is empty, otherwise false.
pubsizeReturns the number of elements in the queue.

Modifiers

pubpushInserts a new element at the end.
pubemplace (since C++11)Constructs a new element in-place at the end.
pubpopRemoves the first pushed element (the one that would've been returned by queue.front()).
pubswap (since C++11)Swaps two queues.

Member objects

protContainer CThe underlying container. By default std::deque<T>.

Non-member functions

puboperator==
operator!=
operator<
operator>
operator<=
operator>=
operator<=> (C++20)
Lexicographically compares values in a queue.
pubstd::swap (std::queue)An overload for the std::swap algorithm.

Helper classes

pubstd::uses_allocator (std::queue)Specializes the std::uses_allocator type trait.

Deduction guides (since C++17)

Click to expand
// (1)
template< class Container >
queue( Container )
-> queue<typename Container::value_type, Container>;
// (2)
template< class Container, class Alloc >
queue( Container, Alloc )
-> queue<typename Container::value_type, Container>;

(1) and (2) allow deduction from an underlying container type

(2) uses std::deque<typename std::iterator_traits<InputIt>::value_type> as an underlying container type (see (4))

// (3) (since C++23)
template< class InputIt >
queue( InputIt, InputIt )
-> queue<typename std::iterator_traits<InputIt>::value_type>;
// (4) (since C++23)
template< class InputIt, class Alloc >
queue( InputIt, InputIt, Alloc )
-> queue<typename std::iterator_traits<InputIt>::value_type,
std::deque<typename std::iterator_traits<InputIt>::value_type, Alloc>>;

(3) and (4) allow deduction from a iterator range

Overload resolution

In order for any of the deduction guides to participate in overload resolution, the folllowing requirements must be met:

zanotuj

The extent to which the library determines that a type does not satisfy LegacyInputIterator is unspecified, except that as a minimum:

  • Integral types do not qualify as input iterators.

Likewise, the extent to which it determines that a type does not satisfy Allocator is unspecified, except that as a minimum:

Examples

Basic manipulation

Push and pop on queue, printing the values
#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();
}
}
Result
1
2
3
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.
uwaga

Note, this article is not finished! You can help by editing this doc page.

Overview

template< class 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

The std::queue class is a container adapter that gives the programmer the functionality of a queue - 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.

std::queue

Defined inqueue

Template parameters

pubT

The type of the stored elements.

uwaga

The behaviour is undefined if T is not Container::value_type.

pubContainer

The type of the underlying container used to store the elements. By default std::deque<T>.

The container must satisfy SequenceContainer
Additionally, it must provide the following functions with the usual semantics:

  • back()
  • front()
  • push_back()
  • pop_front()

The standard containers std::deque and std::list satisfy these requirements.

Type names

pubcontainer_typeContainer
pubvalue_typeContainer::value_type
pubsize_typeContainer::size_type
pubreferenceContainer::reference
pubconst_referenceContainer::const_reference

Member functions

pub(constructors)

Constructs a queue.

pub(destructor)

Destructs a queue.

puboperator=Assigns one queue to another.

Element access

pubfrontAccesses the element at the top (first pushed).
pubbackAccesses the element at the back (last pushed).

Capacity

pubemptyReturns true if the queue is empty, otherwise false.
pubsizeReturns the number of elements in the queue.

Modifiers

pubpushInserts a new element at the end.
pubemplace (since C++11)Constructs a new element in-place at the end.
pubpopRemoves the first pushed element (the one that would've been returned by queue.front()).
pubswap (since C++11)Swaps two queues.

Member objects

protContainer CThe underlying container. By default std::deque<T>.

Non-member functions

puboperator==
operator!=
operator<
operator>
operator<=
operator>=
operator<=> (C++20)
Lexicographically compares values in a queue.
pubstd::swap (std::queue)An overload for the std::swap algorithm.

Helper classes

pubstd::uses_allocator (std::queue)Specializes the std::uses_allocator type trait.

Deduction guides (since C++17)

Click to expand
// (1)
template< class Container >
queue( Container )
-> queue<typename Container::value_type, Container>;
// (2)
template< class Container, class Alloc >
queue( Container, Alloc )
-> queue<typename Container::value_type, Container>;

(1) and (2) allow deduction from an underlying container type

(2) uses std::deque<typename std::iterator_traits<InputIt>::value_type> as an underlying container type (see (4))

// (3) (since C++23)
template< class InputIt >
queue( InputIt, InputIt )
-> queue<typename std::iterator_traits<InputIt>::value_type>;
// (4) (since C++23)
template< class InputIt, class Alloc >
queue( InputIt, InputIt, Alloc )
-> queue<typename std::iterator_traits<InputIt>::value_type,
std::deque<typename std::iterator_traits<InputIt>::value_type, Alloc>>;

(3) and (4) allow deduction from a iterator range

Overload resolution

In order for any of the deduction guides to participate in overload resolution, the folllowing requirements must be met:

zanotuj

The extent to which the library determines that a type does not satisfy LegacyInputIterator is unspecified, except that as a minimum:

  • Integral types do not qualify as input iterators.

Likewise, the extent to which it determines that a type does not satisfy Allocator is unspecified, except that as a minimum:

Examples

Basic manipulation

Push and pop on queue, printing the values
#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();
}
}
Result
1
2
3
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.