class Stack<ValueType>
push
(add to top) and pop
(remove from top). Both of these operations run in O(1) time.
Stacks do not allow access to elements by index, nor can you iterate through the elements using the range-based for loop.
Constructor | ||
O(1) | Initializes a new empty stack. | |
Methods | ||
O(1) | Removes all elements from this stack. | |
O(N) | Returns true if the two stacks contain the same elements in the same order. |
|
O(1) | Returns true if this stack contains no elements. |
|
O(1) | Returns the element on top of this stack, without removing it. | |
O(1) | Removes the element on top of this stack and returns it. | |
O(1) | Adds value onto this stack. |
|
O(1) | Returns the number of values in this stack. | |
O(N) | Returns a printable string representation of this stack. | |
Operators | ||
O(N) | Returns true if stack1 and stack2 contain the same elements. |
|
O(N) | Returns true if stack1 and stack2 are different. |
|
O(N) | Outputs the contents of the stack to the given output stream. | |
O(N) | Reads the contents of the given input stream into the stack. |
Stack();
Usage:
Stack<ValueType> stack; Stack<ValueType> stack = { bottomValue, middleValue, topValue };
void clear();
Usage:
stack.clear();
bool equals(const Stack& stack) const;
true
if the two stacks contain the exact same element values in the same order.
Identical in behavior to the ==
operator.
Usage:
if (stack1.equals(stack2)) ...
bool isEmpty() const;
true
if this stack contains no elements.
Usage:
if (stack.isEmpty()) ...
const ValueType& peek() const;
Usage:
ValueType top = stack.peek();
ValueType pop();
Usage:
ValueType top = stack.pop();
void push(const ValueType& value);
Usage:
stack.push(value);
int size() const;
Usage:
int n = stack.size();
string toString() const;
"{value1, value2, value3}"
.
Usage:
string str = stack.toString();
ostream& operator<<(const Stack& stack);Outputs the contents of
stack
to the given output stream.
The output is in the form {value1, value2, value3}
where elements are listed left-to-right from the bottom of the stack to the top.
Usage:
cout << stack << endl;
istream& operator>>(Stack& stack);Reads the contents of the given input stream into
stack
. Any previous
contents of the stack are replaced.
The input is expected to be in the form {value1, value2, value3}
where elements are listed left-to-right from the bottom of the stack to the top. If unable to read a proper
stack from the stream, the operation results in a stream fail state.
Usage:
if (infile >> stack) ...