Description : push() function is used to insert an element at the top of the stack. The element is added to the stack container and the size of the stack is increased by 1.
Example:
// Empty stack
stack<int> mystack;
//pushing elements using push()
mystack.push(0);
mystack.push(1);
mystack.push(2);
while (!mystack.empty()) {
//deleting elements using pop()
cout << ' ' << mystack.top();
mystack.pop();
}
Description : pop() function is used to remove an element from the top of the stack(newest element in the stack). The element is removed to the stack container and the size of the stack is decreased by 1.
Example:
// Empty stack
stack<int> mystack;
//pushing elements using push()
mystack.push(0);
mystack.push(1);
mystack.push(2);
while (!mystack.empty()) {
//deleting elements using pop()
cout << ' ' << mystack.top();
mystack.pop();
}
Description : empty() function is used to check if the stack container is empty or not.
Example:
// Empty stack
stack<int> mystack;
//pushing elements using push()
mystack.push(0);
mystack.push(1);
mystack.push(2);
while (!mystack.empty()) {
//deleting elements using pop()
cout << ' ' << mystack.top();
mystack.pop();
}
Description : top() function is used to reference the top(or the newest) element of the stack.
Example:
// Empty stack
stack<int> mystack;
//pushing elements using push()
mystack.push(0);
mystack.push(1);
mystack.push(2);
while (!mystack.empty()) {
//deleting elements using pop()
cout << ' ' << mystack.top();
mystack.pop();
}
Description : size() function is used to return the size of the satck or the number of elements in the stack.
Example:
// Empty stack
std::stack<int> mystack;
mystack.push(4);
mystack.push(5);
mystack.push(9);
mystack.push(1);
mystack.push(3);
// Stack from top to bottom becomes 3, 1, 9, 5, 4
// Print the size of mystack (5)
std::cout << mystack.size();