queue::push, queue::pop, queue::front() & queue::empty() in C++ STL

We have explained the use of queue::push, queue::pop, queue::front() & queue::empty() in Queue container in C++ STL with the use of the function in 5 C++ code examples.

Table of content:

  1. Basic of Queue Data Structure
  2. queue::push()
  3. queue::pop()
  4. queue::front()
  5. queue::empty()
  6. Example 1: Insert integer elements in the queue and print the queue
  7. Example 2: Insert string elements in the queue and print the queue
  8. Example 3: Reverse the queue
  9. Example 4: Implementation of push and pop on user defined objects
  10. Example 5: Calculate the size of queue
  11. Application of Queue
  12. Question

We will get started now.

Basic of Queue Data Structure

Queue is a linear data structure that is used to store elements.

It is open at both its ends, one end is used to insert elements called "Rear" and other end is used to delete elements called "Front".Front end points to the beginning of the queue and Rear end points to the end of the queue.

It follows FIFO(First-In-First-Out) structure where the element inserted first will also be removed first.Some of the real-world examples of queue are single-lane road and we can see queue at the ticket counter.

In C++'s Standard Template Library(STL),we have queue as a container.

1-11

queue::push()

push() function is used to insert an element at the back of the queue.

Syntax:

queue_name.push(value)
  • Parameters: Value of the element to be inserted in the queue is passed as a parameter.
  • Return: It doesn't return anything.
  • Result: Add an element at the back of the queue.

Errors and Exceptions:

  • Shows error if the value passed doesn't match the queue type.
  • No exception is thrown if the parameter doesn't throw any exception.

queue::pop()

pop() function is used to remove an element from the front of the queue i.e. the oldest element in the queue is being removed.

Syntax:

queue_name.pop()
  • Parameters: No parameters are passed.
  • Return: It doesn't return anything.
  • Result: Front element is being removed from the queue.

Errors and Exceptions:

  • Shows error if a parameter is passed.
  • No exception is thrown.

To understand the following code,let's learn about some functions being supported by queue.

queue::front()

front() function is used to fetch the first element of a queue.

Syntax:

queue_name.front()
  • Parameters: No parameters are passed.
  • Return: It returns the oldest element present in the queue.
  • Result: Direct reference to the first element of the queue container.

Errors and Exceptions:

  • It causes undefined behaviour if the queue container is empty.
  • If the queue is not empty then no exception will be thrown.

queue::empty()

This function is used to check if the queue container is empty or not.

Syntax:

queue_name.empty()
  • Parameters: No parameters are passed.
  • Return: True,if queue is empty
    else False.

Example 1: Insert integer elements in the queue and print the queue

In this example,we will use push function to insert elements in the queue and then using pop and front function we will print the elements being inserted in the queue.
Here, elements will be of integer type.

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

int main(){
   //An empty queue is being constructed.
   queue<int> q;    
   q.push(1);
   q.push(9);
   q.push(20);
   q.push(4);

   // Print the contents of queue.
   while(!q.empty()){
       cout<<q.front()<<" ";
       q.pop();
   }
   cout<<endl;
}

Output :

1 9 20 4

Example 2: Insert string elements in the queue and print the queue

In this example, we will again use push function to insert elements in the queue and then using pop and front function we will print the elements present in the queue.

Here, elements will be of string type.

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

int main(){
    //An empty queue is being constructed.
    queue<string> q;
    q.push("Welcome");
    q.push("to");
    q.push("my");
    q.push("world");

    //Print content of queue.
    while(!q.empty()){
        cout<<q.front()<<" ";
        q.pop();
     }
     cout<<endl;
}

Output:

Welcome to my world

Example 3: Reverse the queue

In this example,we will first of all using push function insert some elements in the queue and then using recursion we will try the reverse the order of elements in the same queue.Finally print the queue which will be printed in reverse order as we have inserted it.

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

//Function to reverse the queue
void reverseQueue(queue<int> &q){
        if(q.empty()){
            return;
        }
        int x=q.front();
        q.pop();
        reverseQueue(q);
        q.push(X);
 }
 int main(){
     //An empty queue is being constructed.
     queue<int> q;
     q.push(1);       //1 is at the front of the queue.
     q.push(2);
     q.push(3);
     q.push(4);

     reverseQueue(q);
     while(!q.empty()){
         cout<<q.front()<<" ";
         q.pop();
     }
     cout<<endl;
}

Output:

4 3 2 1 // 4 is at the front of the queue.

Example 4: Implementation of push and pop on user defined objects

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

class Student{
    int id;
    public:
    Student(int id){
        this->id=id;
    }
    int getid(){
        return id;
    }
};
int main(){
   Student s1(20);
   Student s2(21);
   Student s3(22);
   //An empty queue is being created.
   queue<Student> q;
   q.push(s1);
   q.push(s2);
   q.push(s3);
   while(!q.empty()){
       Student tmp=q.front();
       cout<<tmp.getid()<<" ";
       q.pop();
   }
   cout<<endl;
}

Output:

20 21 22

Example 5: Calculate the size of queue

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

class Student{
    int id;
    public:
    Student(int id){
        this->id=id;
    }
};
int main(){
    Student s1(1);
    Student s2(2);
    Student s3(3);
    Student s4(4);
    //An empty queue is built.
    queue<Student> q;
    q.push(s1);
    q.push(s2);
    q.push(s3);
    q.push(s4);
    int size=0; //This stores the size of queue.
    while(!q.empty()){
        size++;
        q.pop();
    }
    cout<<size<<endl;
}

Output:

4

Application of Queue

  • Various CPU Scheduling Algorithms are implemented using queue data structure like Round Robin Algorithm.
  • In graph data structure,Breadth First Search(in which each node first try to explore it's neighbour nodes and then move on to the next level) also uses queue data structure.

Question

Queue is initialized and following operations are being performed:
push(20),push(67),push(37),push(4),pop(),pop(),pop().Now what will be the size of the queue?

Options are:

  • 2
  • 3
  • 1
  • None of these

With this article at OpenGenus, you must have the complete idea of queue::push and queue::pop in C++ STL.