Stack - STL Container class
CoderSource.net
Stack - STL Container class - Article by consulttoday
Level: BeginnerType: Article
Rating: Page: 1 of 2

Date: 1/20/2006 12:00:00 AM

Environment: C++, Unix, Windows

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

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us