VMS Help
CXXLSTD, Containers, stack

 *Conan The Librarian

 			   Standard C++	Library
 		 Copyright 1996, Rogue Wave Software, Inc.

 NAME

   stack	 - A container adapter which behaves like a stack (last	in,
   first out).

 SYNOPSIS

   #include <stack>

   template <class T, class Container = deque<T>	>
   class	stack ;

 DESCRIPTION

   The stack container adapter causes a container to behave like	a
   "last	in, first	out" (LIFO)  stack.  The last item that	was
   put	("pushed") onto	the stack	is the first item removed
   ("popped" off).  The stack can adapt to any container that provides
   the operations, back(),  push_back(),	and pop_back().  In
   particular,  deque , list , and vector	can be used.

 INTERFACE

   template <class T, class Container = deque<T>	>
   class	stack {

   public:

   // typedefs

     typedef typename Container::value_type value_type;
     typedef typename Container::size_type size_type;
     typedef typename Container::allocator_type allocator_type

   // Construct

     explicit stack (const allocator_type& = allocator_type());
     allocator_type get_allocator () const;

   // Accessors

     bool empty () const;
     size_type size () const;
     value_type&	top ();
     const value_type& top () const;
     void push (const value_type&);
     void pop ();
   };

   // Non-member	Operators

   template <class T, class Container>
   bool operator== (const stack<T, Container>&,
 		   const stack<T, Container>&);

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

   template <class T, class Container>
   bool operator< (const	stack<T, Container>&,
 		  const	stack<T, Container>&);

   template <class T, class Container>
   bool operator> (const	stack<T, Container>&,
 		  const	stack<T, Container>&);

   template <class T, class Container>
   bool operator<= (const stack<T, Container>&,
 		  const	stack<T, Container>&);

   template <class T, class Container>
   bool operator>= (const stack<T, Container>&,
 		  const	stack<T, Container>&);

 CONSTRUCTOR

   explicit
   stack(const allocator_type& alloc = allocator_taype());
      Constructs	an empty stack.	The stack will use the allocator alloc
      for all storage management.

 ALLOCATOR

   allocator_type
   get_allocator() const;
        Returns a copy of the allocator used by self for	storage
        management.

 MEMBER FUNCTIONS

   bool
   empty() const;
      Returns true if the stack is empty, otherwise false.

   void
   pop();
      Removes the item at the top of the	stack.

   void
   push(const value_type& x);
      Pushes x onto the stack.

   size_type
   size() const;
      Returns the number	of elements on the stack.

   value_type&
   top();
      Returns a reference to the	item at	the top	of the stack.  This
      will be  the last item pushed onto the stack unless	pop()
      has been called since then.

   const	value_type&
   top()	const;
      Returns a constant	reference to the item at the top of the	stack
      as a const value_type.

 NON-MEMBER OPERATORS

   template <class T, class Container>
    bool	operator==(const stack<T, Container>& x,
 		    const stack<T, Container>& y);
      Equality	operator.  Returns true	if x is	the same as y.

   template <class T, class Container>
    bool	operator!=(const stack<T, Container>& x,
 		    const stack<T, Container>& y);
      Inequality operator.  Returns !(x==y).

   template <class T, class Container>
    bool	operator<(const	stack<T, Container>& x,
 		   const stack<T, Container>& y);
      Returns true if the stack	defined	by the elements	contained in
      x is lexicographically less than  the stack defined by the
      elements of y.

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

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

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

 EXAMPLE

   //
   // stack.cpp
   //
    #include <stack>
    #include <vector>
    #include <deque>
    #include <string>
    #include <iostream.h>

   int main(void)
    {
      //	Make a stack using a vector container
      stack<int,vector<int> > s;

      //	Push a couple of values	on the stack
     s.push(1);
     s.push(2);
     cout << s.top() << endl;

      //	Now pop	them off
     s.pop();
     cout << s.top() << endl;
     s.pop();

      //	Make a stack of	strings	using a	deque
      stack<string,deque<string>	> ss;

      //	Push a bunch of	strings	on then	pop them off
     int	i;
     for	(i = 0;	i < 10;	i++)
      {
       ss.push(string(i+1,'a'));
       cout << ss.top() << endl;
      }
     for	(i = 0;	i < 10;	i++)
      {
       cout << ss.top() << endl;
       ss.pop();
      }

     return 0;
    }
   Output :
   2
   1
   a
   aa
   aaa
   aaaa
   aaaaa
   aaaaaa
   aaaaaaa
   aaaaaaaa
   aaaaaaaaa
   aaaaaaaaaa
   aaaaaaaaaa
   aaaaaaaaa
   aaaaaaaa
   aaaaaaa
   aaaaaa
   aaaaa
   aaaa
   aaa
   aa
   a

 WARNINGS

   If your compiler does	not support template parameter defaults,  you
   are required to supply a template	parameter for Container.   For
   example:

   You would not	be able	to write,

   stack<int> var;

   Instead, you would have to write,

   stack<int, deque<int>	> var;

 SEE ALSO

   allocator, Containers, deque,	list, vector

 STANDARDS CONFORMANCE
   ANSI X3J16/ISO WG21 Joint C++	Committee
  Close     Help