The <queue>
header declares the queue
and priority_queue
container adapters. These class templates are not containers in their own rights, but adapt other containers to present the behavior of a queue or priority queue.
A queue is a sequence of items that supports insertion at one end and removal from the other end. Because the first item inserted into a queue is the first item removed, a queue is sometimes called a FIFO (first-in, first-out).
A priority queue is a form of queue, but instead of preserving FIFO order, a priority queue maintains heap order, which ensures the largest item is always first.
See Chapter 11 for information about containers in general.
Compare queues for equalilty
template <typename T, typename Container> bool operator==(const queue<T, Container>& x, const queue<T, Container>& y);
Compares two queues for equality by comparing the adapted containers, e.g., the return value is x.c
==
y.c
.
Compare queues for inequalilty
template <typename T, typename Container> bool operator!=(const queue<T, Container>& x, const queue<T, Container>& y);
Compares two queues for inequality by comparing the adapted containers, e.g., the return value is x.c
!=
y.c
.
Compare queues for less than
template <typename T, typename Container> bool operator<(const queue<T, Container>& x, const queue<T, Container>& y);
Compares two queues by comparing the adapted containers, e.g., the return value is x.c
<
y.c
.
Compare queues for less than or equal
template <typename T, typename Container> bool operator<=(const queue<T, Container>& x, const queue<T, Container>& y);
Compares two queues by comparing the adapted containers, e.g., the return value is x.c
<=
y.c
.
Compare queues for greater than
template <typename T, typename Container> bool operator>(const queue<T, Container>& x, const queue<T, Container>& y);
Compares two queues by comparing the adapted containers, e.g., the return value is x.c
>=
y.c
.
Compare queues for greater than or equal
template <typename T, typename Container> bool operator>=(const queue<T, Container>& x, const queue<T, Container>& y);
Compares two queues by comparing the adapted containers, e.g., the return value is x.c
>=
y.c
.
Priority queue container adapter
template <typename T, typename Container = vector<T>, typename Compare = less<typename Container::value_type> > class priority_queue { public: typedef typename Container::value_type value_type; typedef typename Container::size_type size_type; typedef Container container_type; explicit priority_queue(const Compare& x = Compare(), const Container& = Container()); template <class InputIterator> priority_queue(InputIterator first, InputIterator last, const Compare& x = Compare(), const Container& = Container()); bool empty() const { return c.empty(); } size_type size() const { return c.size(); } const value_type& top() const { return c.front(); } void push(const value_type& x); void pop(); protected: Container c; Compare comp; };
The priority_queue
class template is an adapater for any sequence container that supports random access, such as deque
and vector
. The priority queue keeps its elements in heap order, so it requires a comparator (the Compare
template parameter).
Because priority_queue
is not itself a standard container, it cannot be used with the standard algorithms. (In particular, note the lack of begin()
and end()
members.) Thus, the priority_queue
adapter is useful only for simple needs.
Most of the members of priority_queue
are straightforward mappings from the simple queue protocol to the underlying container protocol. The members are as follows:
explicit
priority_queue
(const
Compare&
cmp
=
Compare(),
const Container& cont = Container())
cont
to the data member c
and cmp
to comp
, and then calls make_heap(c.begin(),
c.end(),
comp)
to initialize the priority queue.template <class InputIter>
priority_queue
(InputIter first,
InputIter last,
const
Compare&
cmp
=
Compare(),
const
Container& cont =
Container())
cont
to the data member c
and cmp
to comp
, and then adds the elements [first
, last) to the container by calling c.insert(c.end(),
first,
last)
, and finally initializes the priority queue by calling make_heap(c.begin(),
c.end(),
comp)
.
empty
() const
pop
()pop_heap
and then erasing the last element in the container.
push
(const value_type& x)x
to the container and then calls push_heap
to ensure the priority queue order.
size
() const
top
() constmake_heap, pop_heap, push_heap in <algorithm>, list in <list>, vector in <vector>
Queue container adapter
template <class T, class Container = deque<T> > class queue { public: typedef typename Container::value_type value_type; typedef typename Container::size_type size_type; typedef Container container_type; explicit queue(const Container& = Container()); bool empty() const { return c.empty(); } size_type size() const { return c.size(); } value_type& front() { return c.front(); } const value_type& front() const { return c.front(); } value_type& back() { return c.back(); } const value_type& back() const { return c.back(); } void push(const value_type& x) { c.push_back(x); } void pop() { c.pop_front(); } protected: Container c; };
The queue
class template is an adapter for any sequence container that supports the front()
, back()
, push_back()
, and pop_front()
members. See the list
and deque
class templates for the standard containers that are suitable.
Because queue
is not itself a standard container, it cannot be used with the standard algorithms. (In particular, note the lack of begin()
and end()
members.) Thus, the queue
adapter is useful only for simple needs.
Most of the members of queue are straightforward mappings from the simple queue protocol to the underlying container protocol. The members are as follows:
explicit
queue
(const Container& cont = Container())
cont
and copies its contents into the queue. With no argument, the constructor creates a new, empty container for the queue.value_type&
back
()
const value_type&
back
()
const
bool
empty
()
const
value_type&
front
()
const value_type&
front
()
const
void
pop
()
void
push
(const value_type& x)
x
to the end of the queue.size_type
size
()
const
deque in <deque>, list in <list>, stack in <stack>