.\" ident @(#)stack.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH stack 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2stack\fP \ - A container adapter that behaves like a stack (last in, first out). .SH SYNOPSIS .br #include .br template > .br class stack ; .SH 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 includes the operations \f2back()\fP, \f2push_back()\fP, and \f2pop_back()\fP. In particular, deque, list, and vector can be used. .SH INTERFACE .br template > .br class stack { .br public: .br // typedefs .br typedef typename Container::value_type value_type; .br typedef typename Container::size_type size_type; .br typedef Container container_type; .br // Construct .br explicit stack (const Container& = Container()); .br // Accessors .br bool empty () const; .br size_type size () const; .br value_type& top (); .br const value_type& top () const; .br void push (const value_type&); .br void pop (); .br }; .br .br // Non-member Operators .br template .br bool operator== (const stack&, .br const stack&); .br template .br bool operator!= (const stack&, .br const stack&); .br template .br bool operator< (const stack&, .br const stack&); .br template .br bool operator> (const stack&, .br const stack&); .br template .br bool operator<= (const stack&, .br const stack&); .br template .br bool operator>= (const stack&, .br const stack&); .SH CONSTRUCTORS .br explicit .br stack(const Container& = Container()); .RE .RS 3 Constructs an empty stack. The stack uses the allocator \f2alloc\fP for all storage management. .RE .SH MEMBER FUNCTIONS .br bool .br empty() const; .RE .RS 3 Returns \f2true\fP if the stack is empty, otherwise \f2false\fP. .RE .br void .br pop(); .RE .RS 3 Removes the item at the top of the stack. .RE .br void .br push(const value_type& x); .RE .RS 3 Pushes \f2x\fP onto the stack. .RE .br size_type .br size() const; .RE .RS 3 Returns the number of elements on the stack. .RE .br value_type& .br top(); .RE .RS 3 Returns a reference to the item at the top of the stack. This is the last item pushed onto the stack unless \f2pop()\fP has been called since then. .RE .br const value_type& .br top() const; .RE .RS 3 Returns a constant reference to the item at the top of the stack as a \f2const value_type\fP. .RE .SH NON-MEMBER OPERATORS .br template .br bool operator==(const stack& x, .br const stack& y); .RE .RS 3 Returns \f2true\fP if \f2x\fP is the same as \f2y\fP. .RE .br template .br bool operator!=(const stack& x, .br const stack& y); .RE .RS 3 Returns \f2!(x==y)\fP. .RE .br template .br bool operator<(const stack& x, .br const stack& y); .RE .RS 3 Returns \f2true\fP if the stack defined by the elements contained in \f2x\fP is lexicographically less than the stack defined by the elements of \f2y\fP. .RE .br template .br bool operator>(const stack& x, .br const stack& y); .RE .RS 3 Returns \f2y < x\fP. .RE .br template .br bool operator<=(const stack& x, .br const stack& y); .RE .RS 3 Returns \f2!(y < x)\fP. .RE .br template .br bool operator>=(const stack& x, .br const stack& y); .RE .RS 3 Returns \f2!(x < y)\fP. .RE .SH EXAMPLE .br // .br // stack.cpp .br // .br #include .br #include .br #include .br #include .br #include .br using namespace std; .br .br int main(void) .RE .RS 1 { .RE .RS 0 // Make a stack using a vector container .br stack > s; .br // Push a couple of values on the stack .br s.push(1); .br s.push(2); .br cout << s.top() << endl; .br .br // Now pop them off .br s.pop(); .br cout << s.top() << endl; .br s.pop(); .br .br // Make a stack of strings using a deque .br stack > ss; .br // Push a bunch of strings on then pop them off .br int i; .br for (i = 0; i < 10; i++) .RE .RS 3 { .br ss.push(string(i+1,'a')); .br cout << ss.top() << endl; .br } .RE .RS 0 for (i = 0; i < 10; i++) .RE .RS 3 { .br cout << ss.top() << endl; .br ss.pop(); .br } .RE .RS 0 .br return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 2 .br 1 .br a .br aa .br aaa .br aaaa .br aaaaa .br aaaaaa .br aaaaaaa .br aaaaaaaa .br aaaaaaaaa .br aaaaaaaaaa .br aaaaaaaaaa .br aaaaaaaaa .br aaaaaaaa .br aaaaaaa .br aaaaaa .br aaaaa .br aaaa .br aaa .br aa .br a .SH WARNINGS If your compiler does not support template parameter defaults, you are required to supply a template parameter for \f2Container\fP. For example: You would not be able to write, \f2stack var;\fP Instead, you would have to write, \f2stack > var;\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO allocator, Containers, deque, list, vector