Apache C++ Standard Library Reference Guide

## stack

Library: Containers

    Does not inherit

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Constructors
- Member Functions
- Nonmember Operators
- Example
- See Also
- Standards Conformance

### Local Index

#### Members

empty() pop() | push() size() | stack() top() |

#### Non-Members

operator!=() operator>() | operator>=() operator<() | operator<=() operator==() |

### Summary

A container adapter that behaves like a stack (last in, first out)

### Synopsis

    #include <stack>

    namespace std {
      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 put (pushed) onto the stack is the first item removed (popped off). The stack can adapt any container that includes the operations `back()`, `push_back()`, and `pop_back()`. In particular, deque, list, and vector can be used.

### Interface

    namespace std {

      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 Container container_type;

      // Constructor
      explicit stack(const Container& = Container());

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

      // Nonmember 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>&);
    }

### Constructors

    explicit
    stack(const Container& = Container());

Constructs an empty stack.

### 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.

    const value_type&
    top() const;

Returns a constant reference to the item at the top of the stack as a `const value_type`.

### Nonmember Operators

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

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);

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>

    int main ()
    {
    #ifndef _RWSTD_NO_NAMESPACE
      using namespace std;
    #endif

      // Make a stack using a vector container.

      stack<int,vector<int,allocator<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,allocator<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;
    }

    Program Output:

    2
    1
    a
    aa
    aaa
    aaaa
    aaaaa
    aaaaaa
    aaaaaaa
    aaaaaaaa
    aaaaaaaaa
    aaaaaaaaaa
    aaaaaaaaaa
    aaaaaaaaa
    aaaaaaaa
    aaaaaaa
    aaaaaa
    aaaaa
    aaaa
    aaa
    aa
    a

### See Also

allocator, Containers, deque, list, vector

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 23.2.3.3