Name
stack class template — Stack container adapter
Synopsis
template <class T, class Container = deque<T> > class stack { public: typedef typename Container::value_type value_type; typedef typename Container::size_type size_type; typedef Container container_type; protected: Container c; public: explicit stack(const Container& = Container( )); bool empty( ) const { return c.empty( ); } size_type size( ) const { return c.size( ); } value_type& top( ) { return c.back( ); } const value_type& top( ) const { return c.back( ); } void push(const value_type& x) { c.push_back(x); } void pop( ) { c.pop_back( ); } };
The stack
class template is
an adapter for any sequence container—such as deque
, list
, and vector
—that supports the back
, push_back
, and pop_back
members. (The default is deque
.)
Because stack
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 stack
adapter is useful only for simple
needs.
Most of the members of stack
are straightforward mappings from a
simple stack protocol to the underlying container protocol. The
members are:
explicit
stack
(const
Container&
cont
=
Container( ))
Copies the elements from
cont
to thec
data memberbool
empty
( )
const
Returns
true
if the stack is emptyvoid
pop
( )
Erases the item at the top of the stack
void
push
(const value_type& x)
Adds
x
at the top of the stacksize_type
size
( )
const
Returns the number of items in the stack
value_type& ...
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.