How to use STL Stack in C++

Learn about how to use STL Stack in C++. The syntax to start using stack in C++ is std::stack.

In this tutorial, I will discuss the uses, applications, and examples for stacks in C++. After this, you will be able to solve most of the stack-related algorithm questions without implementing your own stack data structure. That is by using the inbuild STL std::stack in C++.

What is Stack?

Stacks are a sort of container adapter that works on the LIFO (Last In First Out) principle, in which a new element is added to one end (top) and an element is deleted from the opposite end (bottom).

The underlying container of a stack is an encapsulated object of either vector or deque (by default) or list (sequential container class), with a specific set of member functions to access its elements.

Syntax

To use the STL you have to include the stack file. But if you are using standard c++ 11 then it comes included in most of the standard library of C++.

#include<stack>

Features of std::stack in c++

STL Stack provides the following methods that are available in the stack library or stack implementation to use.

  • push(s): Push the element s on top of the stack.
  • pop(): This function returns the top-most element and then deletes the element from the stack.
  • top(): Returns the top of the stack element.
  • size(): Returns the total number of elements present at that time in stack.
  • empty(): Return true if stack is empty, Otherwise if stack is not empty false is returned.

Implementation

Let me show is a small implementation of STL std::stack in practice. Similarly, you can use the stack in your applications or coding interview questions.

#include <iostream>
#include <stack>
using namespace std;
 
// Stack implementation with use of `std::stack` in C++.
int main()
{
    stack<string> newStack;
 
    newStack.push("First Push");    // Insert `A` into the stack
    newStack.push("Second Push");    // Insert `B` into the stack
    newStack.push("Third Push");    // Insert `C` into the stack
    newStack.push("Fourth Push");    // Insert `D` into the stack
 
    // prints the top of the stack (`Fourth Push`)
    cout << "The top element is " << newStack.top() << endl;
  
    // returns the total number of elements present in the stack newStack
    cout << "The stack size is " << newStack.size() << endl;
 
    newStack.pop();        // removing the top element (`Fourth Push`)
    newStack.pop();        // removing the next top (`Third Push`)
 
    //Removing all the elements from the stack.
    cout << "The stack size is " << newStack.size() << endl;
 
    // check if the stack is empty
    if (newStack.empty()) {
        cout << "The stack is empty\n";
    }
    else {
        cout << "The stack is not empty\n";
    }
  
  //The Stack is Empty is printed as all the elements present in the stack are poped out.
 
    return 0;
}

If you liked our post related to the stack implementation in c++ then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Wanna read more interview-related questions? Check Top Interview Questions category.

Further Read:

  1. How To Check if a given key exists in a map or not in C++
Share your love

One comment

Leave a Reply