DekGenius.com
Team LiB   Previous Section   Next Section

13.43 <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 last item inserted into a stack is the first item removed, a stack is sometimes called a LIFO (last-in, first-out) container.

See Chapter 10 for information about containers.

operator== function template Compares stacks for equality

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

The == operator compares two stacks for equality by comparing the adapted containers (e.g., the return value is x.c == y.c).

operator!= function template Compares stacks for inequality

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

The != operator compares two stacks for inequality by comparing the adapted containers (e.g., the return value is x.c != y.c).

operator< function template Compares stacks for less-than

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

The < operator compares two stacks by comparing the adapted containers (e.g., the return value is x.c < y.c).

operator<= function template Compares stacks for less-than-or-equal

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

The <= operator compares two stacks by comparing the adapted containers (e.g., the return value is x.c <= y.c).

operator> function template Compares stacks for greater-than

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

The > operator compares two stacks by comparing the adapted containers (e.g., the return value is x.c >= y.c).

operator>= function template Compares stacks for greater-than-or-equal

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

The >= operator 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 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 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>, <list>, <queue>, <vector>

    Team LiB   Previous Section   Next Section