push() in Stack C++ STL


Reading time: 25 minutes | Coding time: 5 minutes

As we know stack data structure supports last in first out (LIFO) approach of managing data, it also becomes important to know about how we can insert elements into it. Here we will discuss about how we can insert elements in stack container using push() method provided by C++ STL (Standard Template Library). Elements are inserted at the top of stack.

We have covered different cases such as:

  • Inserting integers in Stack
  • Inserting user defined objects in Stack
  • Inserting strings in Stack
  • Inserting vectors and pairs in Stack

Basic Syntax of push() :

stack_name.push(element)

Example:

stack <int> container;  
container.push(2);
  • element - It is the variable which contains data to be added in the stack. Data type of element is the type of container which we defined in the template parameter when initializing the stack. The function may throw an error if type of parameter doesn't match with the type of container.

  • return value - none

Example code for stack::push() function

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

int main(){
    //initializing stack to store integer values..
    stack <int> container;  
    
    //adding elements in stack using push method....
    container.push(2);
    container.push(5);
    container.push(3);
    
   //printing elements to visualize stack.....
    while(!container.empty()){
        cout<<container.top()<<" ";
        container.pop();
    }
    
return 0;
}

Output :

3 5 2

Here, 2 will be added at the bottom(start) of the stack then 5 will be added at the top(after 2) and then 3 will be added at the top(after 5). When pop() is called on stack, the element that goes last in the stack will be removed first and following that we have the above output. It is actually reverse of the order with which we have performed insertion.

We can also insert user-defined objects, strings and other containers like vector, pair, etc.

Inserting user defined objects in Stack

In this example C++ code, we created an user defined object and inserted it into the Stack container using the push() function.

We created a Book class as follows:

class Book
{
    public:
    //member variables of class book...
    string Author;
    int id;

    //constructor of Book's instance...
    Book(string Author,int id)
    {
        this->Author = Author;
        this->id = id;
    }
};

It is inserted as follows:

Book b1("OpenGenus",125);
stack <Book> container;
container.push(b1);

Following is the complete C++ code:

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

class Book
{
    public:
    //member variables of class book...
    string Author;
    int id;

    //constructor of Book's instance...
    Book(string Author,int id)
    {
        this->Author = Author;
        this->id = id;
    }
};

int main(){
    //creating instances of Book class...
    Book b1("OpenGenus",125);
    Book b2("OpenSource",345);

    //initializing stack to store Book objects....
    stack <Book> container;

    //adding elements in stack using push method....
    container.push(b1);
    container.push(b2);
    
    //printing details of books in stack...
    while(!container.empty()){
        cout<<container.top().Author<<" "<<container.top().id<<endl;
        container.pop();
    }
    
    return 0;
}

Output :

OpenSource 345
OpenGenus 125

Here we first created a class that has author's name and id of book as data members then we initialized stack to store instances of this class.

Inserting strings in Stack

In this example, we will insert strings in a Stack container. This is done as follows:

stack <string> container;
string s1 = "opengenus";
container.push(s1);

Following is the complete C++ code:

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

int main(){
    //initializing stack to store strings
    stack <string> container;
    string s1 = "first";
    string s2 = "opengenus";

    //adding elements in stack using push method....
    container.push(s1);
    container.push(s2);
    container.push("third");
    container.push("fourth");
    
    //printing strings
     while(!container.empty()){
        cout<<container.top()<<" ";
        container.pop();
    }

    return 0;
}

Output :

fourth third opengenus first

Inserting vectors and pairs in Stack

We can insert any object in a stack. It can be user defined objects or objects from STL as well. In this example, we will see how to insert vectors and pairs in the stack container.

This is done as follows:

stack <vector<int>> vector_container;
vector<int> a;
a.push_back(30);
vector_container.push(a);

Following is the complete C++ code example:

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

int main(){
    //initializing stacks to store vector and pair containers.....
    stack <vector<int>> vector_container;
    stack <pair<int,int>> pair_container;
    
    //initializing vectors
    vector<int> a,b;
    
    a.push_back(30);
    a.push_back(20);
    b.push_back(10);
    //inserting vectors in vector_container stack ....
    vector_container.push(a);
    vector_container.push(b);

    //inserting pairs in pair_container stack....
    //here we can either initialize pair
    //                or
    // we can directly call make_pair function in push() to create pair.
    pair_container.push(make_pair(1,10));
    pair_container.push(make_pair(2,20));
    //another approach to insert pair in pair_container stack...
    pair<int,int> c,d;
    
    //assigning values to  first and second member of pair....
    c.first=3; c.second=30;
    d.first=4; d.second=40;
    
    pair_container.push(c);
    pair_container.push(d);
    
    return 0;
}

We will go through some questions to solidify your understanding.

Question 1

Which of the following applications does not use stack?

Infix to postfix conversion of an expression
Parenthesis balancing program
Recursion
CPU task scheduling
CPU uses queue to execute tasks according to their order in queue.

Question 2

If the sequence of operations - push(1), push(2), pop(), push(1), push(2), pop(), pop(), push(2) are performed on a stack then size of stack after these operations is:

3
7
5
2
There are 5 push and 3 pop so it results in 2 elements in the stack after all operations.

With this, you should have a complete idea of push() function in Stack container of C++ Standard Template Library (STL).