T
- Type of elements.
Return: none
Arguments: T
Pushes element with a given value on the top of a stack.
Return: none
Arguments: none
Removes element from the top of a stack.
Return: T
Arguments: none
Gets value of the top element.
Return: unsigned int
Arguments: none
Gets size of the stack.
Return: bool
Arguments: none
Returns whether the stack is empty.
Return: iterator
Arguments: none
Returns iterator object to the first element.
Return: iterator
Arguments: none
Returns iterator object to the element past the last.
Library include
#include "path/to/Basic-Data-Structures/include/stack"
Stack declaration
Stack<int> s;
Basic push
, pop
and top
usage
s.push(4); // stack: 4
s.push(1); // stack: 1 4
s.pop(); // stack: 4
s.push(3); // stack: 3 4
std::cout << s.top() << "\n"; // prints: 3
s.push(2); // stack: 2 3 4
std::cout << s.top() << "\n"; // prints: 2
Print stack using empty
, top
and pop
while(!s.empty()) {
std::cout << s.top() << "\n";
s.pop();
}
Print stack using iterator
(recommended way)
for(Stack<int>::iterator i = s.begin(); i != s.end(); ++i) {
std::cout << *i << "\n";
}
Print stack using iterator
with range-based for loop
and auto
(recommended way for C++11 and higher)
for(auto i : s) {
std::cout << i << "\n";
}