All about stack STL in c++ for competitive programming( initialization, traversal and methods/functions ) :

stack STL

Important Points:

  1. It is a type of container adaptor which follows LIFO (Last In First Out) rule.
  2. Elements are inserted and extracted from the top of the container only.
  3. It is same as the stack in c but with a huge advantage of availability of direct functions/methods for many operations.
  4. Remember to use the header file #include<stack> before using stack STL.

How do i remember LIFO ?

  • Assume a old version CD holder stand (a pillar on a circular base) in which we can put CDs.
  • Now think of the CDs as elements and the CD holder as the container.
  • A CD can only be put in the stand from the top and after a CD is put we can put the next CD on top of the previous one.
  • To remove a particular CD we have to first remove the CDs on top of it which means LIFO
    (Last In First Out) i.e the CD which was put in last has to be removed out first.
 

 

stack STL initialization

declaring a stack and using push( ) method to put elements into it from back:  

    stack < int > s ;
    s.push( 10 ) ;
   
s.push( 20 ) ;
    s.push( 30 ) ;
 
    Therefore, 
    s   =  30   (top)
             20
             10   (bottom)
 

 

 

All stack STL methods/ functions  

stack STL important functions:

1. push( ) or emplace( )used to insert a new element into the stack.

2. size( ) : returns the number of elements in the stack.

3. empty( ) : returns true if the container is empty otherwise, false.
 
4. top( ) : returns the top element of the stack.

5. pop( ) : removes the top element of the stack.
 
6. swap( ) : used to exchange the content of two stack containers of same type (size may differ).
 

emplace( ) vs push( ) which to use?
  • emplace() avoids unnecessary copy of objects.
  • for primitive data types it does not matter which one to use but for objects or bigger objects use emplace() for efficiency.
  • unlike push(), no copy or move operation are performed in emplace().
 

 

 all stack STL important functions with example

1. Initializing stack : stack< int > s ;
 
2. Putting elements into it : 
     s.push( 10 ) ;
     s.push( 30 ) ; 
     s.push( 20 ) ;  
     Therefore, 
     s = { 20, 30, 10 } 
 
3. cout << s.size( ) ;               // 3
 
4. cout << s.top( ) ;               //20
 
5. Checking whether stack is empty or not :
    if( s.empty( ) = = false )
    cout << "not empty" ;     //it will be printed
    else
    cout << "empty" ;
 
9. s.pop( ) ;  
     // top element removed i.e 20
 


stack STL traversal method

Let our queue be q :
stack < int > s ;
 
   while ( !s.empty( ) )
    {
          cout << s.top( ) << endl; 
          s.pop( );
    }

       

 
 
 
 

 

 

Hi reader! got any queries or need more explanation for something? Feel free to comment below.

Comments

Popular posts from this blog

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :

HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) :