Open In App

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

Last Updated : 02 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The queue is a type of container that 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. For this purpose, the member functions queue::push() and queue::pop() are used respectively.

In this article, we will discuss these queue::pop() and queue()::push() functions.

queue::push() in C++

The push() function of the queue container 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.

Syntax

queue_name.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.

Example of queue::push()

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

 0 1 2

Time Complexity: O(1) (Queue pop() operation take constant time complexity.)

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

queue::pop() in C++

The pop() function of the queue container is used to remove an element from the front of the queue(the 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

queue_name.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 an error if a parameter is passed.
  2. Shows no exception throw guarantee if the parameter doesn’t throw any exception.

Example of queue::pop()

CPP





Output

 2

Time Complexity: O(1) (Queue pop() operation take constant time complexity.)

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

Application of push() and pop() in C++

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.

Example

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

Time complexity: O(n) // n is the size of the queue.

Auxiliary Space: O(1)

Difference between queue push() and queue pop() in C++

The following table list the differences between queue::push() and queue::pop():

 S. No.

queue push() 

queue pop()

1.

It is used to insert a new element at the end of the queue. It is used to remove the next element in the queue

2.

Its syntax is -:
push (value_type&& val);
Its syntax is -:
pop();

3.

It takes one parameter that is the value to be inserted. It does not take any parameters.

4.

Its return type is void. Its return type is void.

5.

It is present in the <queue> header file. It is present in the <queue> header file.

 



Previous Article
Next Article

Similar Reads

priority_queue::push() and priority_queue::pop() in C++ STL
Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the smallest element at the top by simply passing an extr
3 min read
Stack push() and pop() in C++ STL
Stacks are a type of container adaptors that follow LIFO(Last In First Out) property, where a new element is added at one end and an element(at the top) is removed from that end only. Basically, the insertion and deletion happen on the top of the stack itself. stack::push() push() function is used to insert or 'push' an element at the top of the st
4 min read
queue::empty() and queue::size() in C++ STL
Queue is a type of container adaptor that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::empty() empty() function is used to check if the queue container is empty or not. SyntaxqueueName.empty()ParametersThis function does not accept any parameter.Return Val
4 min read
queue::front() and queue::back() in C++ STL
Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::front() This function is used to reference the first or the oldest element of the queue container. This function can be used to fetch the first element of a queue.Syn
3 min read
How to Push All Elements from a Vector to a Queue in C++?
In C++, vectors are dynamic arrays while the queue is a data structure that follows the FIFO (First In First Out) property. In this article, we will learn how to push all elements from a vector to a queue in C++. Example Input: myVector = {1, 5, 8, 9, 4} Output: myQueue = 1 5 8 9 4Copy All Vector Elements into the Queue in C++We cannot directly pus
2 min read
Difference Between Linear Queue and Circular Queue
Linear Queue: A Linear Queue is generally referred to as Queue. It is a linear data structure that follows the FIFO (First In First Out) order. A real-life example of a queue is any queue of customers waiting to buy a product from a shop where the customer that came first is served first. In Queue all deletions (dequeue) are made at the front and a
4 min read
Advantages of circular queue over linear queue
Linear Queue: A Linear Queue is generally referred to as Queue. It is a linear data structure that follows the FIFO (First In First Out) order. A real-life example of a queue is any queue of customers waiting to buy a product from a shop where the customer that came first is served first. In Queue all deletions (dequeue) are made at the front and a
3 min read
queue::swap() in C++ STL
Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::swap() swap() function is used to exchange the contents of two queu
2 min read
queue::emplace() in C++ STL
Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::emplace() This function is used to insert a new element into the qu
3 min read
Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
Given a binary tree. The task is to find the value of the deep­est node in the given binary tree. Examples: Input: Root of below tree 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Output: 8 Input: Root of below tree 1 / \ 2 3 / 6 Output: 6 Approach: We have already discussed the two different ways to find the deepest node in a binary tree in this post. Here, we wi
6 min read
Practice Tags :