Home >>C++ Standard Template Library Tutorial(STL) >C++ Stack

C++ Stack

Stack in C++

When you are dealing in the field of computer science then you have go for a large variety of programs and each program come with their own set of utility and domain. In this field there is a big number of the data structures from where a programmer can choose but that only depends on the environment and purpose. Among those, 'stack in C++' is known to be very useful.

Here is the syntax of the stack:

template<class T, class Container = deque<T> > class stack;  

Template Parameters

T : This argument generally specifies the element type that the container adaptor will be holding.

Container : This argument generally specifies an internal object of the container that acts as the holding position for the elements of the stack.

Stack in C++ is basically a data structure that is known to work on the LIFO technique, and LIFO expands to Last In First Out. This simply means that the element that was first inserted will be extracted at the end naturally and the circle completes itself multiple times. Top is an element that is at the upper most position. Top is responsible for all the insertion and deletion operations made in the stack. Stacks are generally implied as the container adaptors in the application areas.

Here is the list of the containers whose support is required for these depicted operations:

  • empty()
  • size()
  • back()
  • push_back()
  • pop_back()

C++ Stack Functions

In order to play an object or a variable in the field of programming these functions are used. Stack generally delivers a variety of functions that are used or embedded in the programs. Here is a list of all the functions along with a short description:

Function Description
(constructor) This function is generally used for the construction of a stack container.
empty() This function is generally used to test for the emptiness of a stack. If the stack is found to be empty then the function returns true else false.
size() This function is generally used to return the size of the stack container that is a measure of the number of elements stored in the stack.
top() This function is generally used to access the top element of the stack. The element plays a key role as all the insertion and deletion operations are performed at the top element.
push() This function is generally used for the insertion of a new element at the top of the stack.
pop() This function is generally used for the deletion of element, and generally the element in the stack is deleted from the top.
emplace() This function is generally used for the insertion of new elements in the stack above the current top element.
swap() This function is generally used for interchanging the contents of two containers in reference.
relational operators This non-number function is generally used to specify the relational operators that are needed for the stacks.
uses allocator<stack> This non-number function is generally used to use the allocator for the stacks.

Stack Function example

#include <iostream>  
#include <stack>  
using namespace std;  
void newstack(stack  t)  
{  
    stack  st = t;  
    while (!st.empty())  
    {  
        cout << '\t' << st.top();  
        st.pop();  
    }  
    cout << '\n';  
}  
int main ()  
{  
    stack  newst;  
    newst.push(50);  
    newst.push(40);  
    newst.push(30);  
    newst.push(20);  
    newst.push(10);  
  
    cout << "Here is the value of the  stack : ";  
    newstack(newst);  
    return 0;  
} 
Output :Here is the value of the stack : 10 20 30 40 50