Stack – STL Container class by consulttoday


Stack<> Container class

In this article we will discuss about a Simple Container Class Stack.

This represents a Stack of your data type. <> Stack is modeled using a deque (Or a double ended queue). All the operators < , > , <=, >=, != , = = are overloaded for the stack container class. That means you can check two stacks just like two integers or two inbuilt data types. For example there are two stacks s1 and s2 and you want to compare them, you can write

if(s1==s2) and so on for other operators..

stack<>  – One upon another

Methods of stack class

  • empty()
  • get_allocator()
  • pop()
  • push()
  • size()
  • top()
  • operator ==
  • operator <=
  • operator >=
  • operator !=
  • operator <
  • operator >

Here we will discuss about each method, will show you what it does and then at the end we will give  example where you can use these methods together….

empty() and size()

The method empty() is used to check whether a stack is empty or not. This method returns a bool value. The value is true if the stack is empty, otherwise it will return false value. Here is a code that will make you understand better how empty() is used…

size() returns the number of elements present in the stack.

#include <iostream>
#include <stack>
#include <conio.h>
using namespace std;

int main()
{
stack<char> codes;
codes.push('a');
codes.push('b');
cout<<"The size of the stack is:"<<codes.size<<endl;
//checking whether the stack is empty or not ?
if(codes.empty()==true)
cout<<"The Stack is Empty";//prints the size of the stack
getch();
return 0;
}

You can drop the “true” in the above each block. But it is advisable to write true, because it makes your intention more clear to other programmers..

The output of this program will be 2 since there are 2 elements in the stack.

push() and top()

push() is used to push an element at the stack top. We just need to pass the argument for pushing at the top of the stack. The return type of this method is void. So nothing is returned only the values are pushed at the stack top.

top(), as the name suggests used to pop the MRA(Most Recently Added) element from the stack that is at the top.  The code below puts some integer into an integer stack and then displays the MRA element.

#include <iostream>
#include <stack>
#include <conio.h>
using namespace std;
int main()
{
stack<int> codes;
for(int i=0;i<10;i++)
//pushing elements at the top of the stack.
codes.push(i);
cout<<codes.top()<<endl;//Displaying the top element
getch();
return 0;
}
The output of this code will be 9.

pop()

This method is used for popping up the last added (MRA : Most Recently Added) element of the stack.
The difference  between top() and pop() is that in top() we are not really popping the MRA element but the pop() method pops the most recently added Element. Here is a code involving push(), pop() and top()

#include <iostream>
#include <stack>
using namespace std;

int main()
{
string title;
int howmany;
stack<string> discs;
//Asking the user how many discs he wants to enter in the stack.
//The loop will rotate that many number of times and then
//prompt the user for input.
cout<<"How many discs :";
cin>>howmany;
for(int i=0;i>title;
//pushing the discs one upon the other
discs.push(title);
}
cout<<"Now at the top of the CD Stack we have :"<<discs.top()<<endl;
cout<<"The first one entered is "<<endl;
while(!discs.empty())
{
title = discs.top();
discs.pop();
}
cout<<title<<endl;
return 0;
}

Here is the screenshot of a sample run of the above program

To get the Maximum Possible Size of the Stack

To find the maximum possible size of the stack we first have to get the allocator that is associated with the stack and then we shall have to use max_size() method to find the maximum possible size of the specified stack.

#include <iostream>
#include <stack>
using namespace std;
{
stack<string> discs;
cout<<discs.get_allocator().max_size()<<endl;
return 0;
}

The output of this program is

268435455

———————–

This is independant of the size of the stack. And it is a variable constant. That means for a stack of integer this value will be different, for a float stack this value will be different from integer stack but will be constant for all float type variables.

Applications

Stacks are used frequently in parsers. Very common examples includes “Infix to Postfix conversion”, “Parenthesis Matching”, “Back Tracking (Mostly in Games Programming)” etc..We will take up all these examples in the next article.. Till then happy coding…