Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

queue push() and pop() in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 25 Nov, 2021

The queue is a type of container which operates in a First In First Out (FIFO) type of arrangement. Elements are inserted at the back(end) and are deleted from the front of the queue. 

queue::push()

push() function is used to insert an element at the back of the queue. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <queue> header file. The element is added to the queue container and the size of the queue is increased by 1.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Syntax : 

queuename.push(value)

Parameters: The value of the element to be inserted is passed as the parameter.



Result: Adds an element of value same as that of the parameter passed at the back of the queue.

Examples: 

Input :  myqueue
         myqueue.push(6);
Output : 6

Input :  myqueue
         myqueue.push(0);
         myqueue.push(1);
Output : 0, 1

Errors and Exceptions: 

  1. Shows an error if the value passed doesn’t match the queue type.
  2. Shows no exception throw guarantee if the parameter doesn’t throw any exception.

CPP




// CPP program to illustrate
// Implementation of push() function
  
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    // Empty Queue
    queue<int> myqueue;
    myqueue.push(0);
    myqueue.push(1);
    myqueue.push(2);
  
    // Printing content of queue
    while (!myqueue.empty()) {
        cout << ' ' << myqueue.front();
        myqueue.pop();
    }
}
Output
 0 1 2

Note: Here, output is printed on the basis of FIFO property.

queue::pop()

pop() function is used to remove an element from the front of the queue(oldest element in the queue). This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <queue> header file. The element is removed from the queue container and the size of the queue is decreased by 1.

Syntax : 

queuename.pop()

Parameters: No parameters are passed



Result: Removes the oldest element in the queue or basically the front element.

Examples: 

Input :  myqueue = 1, 2, 3
         myqueue.pop();
Output : 2, 3

Input :  myqueue = 3, 2, 1
         myqueue.pop();
Output : 2, 1

Errors and Exceptions:

  1. Shows error if a parameter is passed.
  2. Shows no exception throw guarantee if the parameter doesn’t throw any exception.

CPP




// CPP program to illustrate
// Implementation of pop() function
  
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    // Empty Queue
    queue<int> myqueue;
    myqueue.push(0);
    myqueue.push(1);
    myqueue.push(2);
    // queue becomes 0, 1, 2
  
    myqueue.pop();
    myqueue.pop();
    // queue becomes 2
  
    // Printing content of queue
    while (!myqueue.empty()) {
        cout << ' ' << myqueue.front();
        myqueue.pop();
    }
}
Output
 2

Note: Here, output is printed on the basis of FIFO property.

Application: push() and pop() 

Given a number of integers, add them to the queue and find the size of the queue without using the size function. 

Input : 5, 13, 0, 9, 4
Output: 5

Algorithm:
 
1. Push the given elements to the queue container one by one. 
2. Keep popping the elements of the queue until the queue becomes empty, and increment the counter variable. 
3. Print the counter variable.

CPP




// CPP program to illustrate
// Application of push() and pop() function
  
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    // Empty Queue
    int c = 0;
  
    queue<int> myqueue;
    myqueue.push(5);
    myqueue.push(13);
    myqueue.push(0);
    myqueue.push(9);
    myqueue.push(4);
    // queue becomes 5, 13, 0, 9, 4
  
    // Counting number of elements in queue
    while (!myqueue.empty()) {
        myqueue.pop();
        c++;
    }
    cout << c;
}
Output
5

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!