Stack - STL Container class

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. 

1 2

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > cplus > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction