queue class template — 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. (The default is deque
.)
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
member functions.) Thus, the queue
adapter is useful only for simple
needs.
Most of the members of queue are straightforward mappings from a simple queue protocol to the underlying container protocol. The members are:
explicit
queue
(const Container& cont
= Container( ))
Takes an existing 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
Returns the last item in the queue, that is, the item that was added most recently to the queue.
bool
empty
( )
const
Returns true
if the
queue is empty.
value_type&
front
( )
, const value_type&
front
( )
const
Returns the first item in the queue.
void
pop
( )
Erases the first item from the queue.
void
push
(const value_type&
x)
Inserts x
at the end
of the queue.
size_type
size
( )
const
Returns the number of items in the queue.