Draft 2002-08-19

<stack>

The <stack> header declares the stack container adapter. This class template is not a container in its own right, but adapts other containers to present the behavior of a stack.

A stack is a sequence of items that supports insertion and removal at one end. Because the larst item inserted into a stack is the first item removed, a stack is sometimes called a LIFO (last-in, first-out).

See Chapter 11 for information about containers in general.

operator== function template

Compare stacks for equalilty

template <typename T, typename Container>
bool operator==(const stack<T, Container>& x,
                const stack<T, Container>& y);

Compares two stacks for equality by comparing the adapted containers, e.g., the return value is x.c == y.c.

operator!= function template

Compare stacks for inequalilty

template <typename T, typename Container>
bool operator!=(const stack<T, Container>& x,
                const stack<T, Container>& y);

Compares two stacks for inequality by comparing the adapted containers, e.g., the return value is x.c != y.c.

operator< function template

Compare stacks for less than

template <typename T, typename Container>
bool operator<(const stack<T, Container>& x,
               const stack<T, Container>& y);

Compares two stacks by comparing the adapted containers, e.g., the return value is x.c < y.c.

operator<= function template

Compare stacks for less than or equal

template <typename T, typename Container>
bool operator<=(const stack<T, Container>& x,
                const stack<T, Container>& y);

Compares two stacks by comparing the adapted containers, e.g., the return value is x.c <= y.c.

operator> function template

Compare stacks for greater than

template <typename T, typename Container>
bool operator>(const stack<T, Container>& x,
               const stack<T, Container>& y);

Compares two stacks by comparing the adapted containers, e.g., the return value is x.c >= y.c.

operator>= function template

Compare stacks for greater than or equal

template <typename T, typename Container>
bool operator>=(const stack<T, Container>& x,
                const stack<T, Container>& y);

Compares two stacks by comparing the adapted containers, e.g., the return value is x.c >= y.c.

stack class template

Stack container adapter

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 adapater for any sequence container that supports the back(), push_back(), and pop_back() members, such as deque, list, and vector.

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() members.) Thus, the stack adapter is useful only for simple needs.

Most of the members of stack are straightforward mappings from the simple stack protocol to the underlying container protocol. The members are as follows:

explicit stack (const Container& cont = Container())
Copies the elements from cont to the c data member.
bool empty () const
Returns true if the stack is empty.
void pop ()
Erases the item at the top of the stack.
void push (const value_type& x)
Adds x at the top of the stack.
size_type size () const
Returns the number of items in the stack.
value_type& top ()
const value_type& top () const
Returns the item at the top of the stack.

See Also

deque in <deque>, list in <list>, vector in <vector>