Container Classes: Stacks
The stack provides a dynamically resizing, LIFO access container for like type objects. Stacks are suitable replacements for custom-developed stacks built from C++ arrays.
Usage: #include <stack> and use the std namespace.
Declaration examples:
stack<int> stackOfIntegers;
stack<char> stackOfCharacters;
stack<myClass> stackOfMyClass; // a stack of myClass objects where myClass is any user-defined class
Common member functions:
Operators: [], =, ==, and !=
push(element): inserts an element onto the top of the stack
top(): returns a copy of the element at the top of the stack
pop(): removes the top element from the stack
size(): reports the number of elements in the stack
empty(): returns a bool to indicate the stackis empty (true) or not empty (false)
Example program:
This program creates a stack to hold a custom class called myC: stacktest.cpp