VOOZH about

URL: https://www.geeksforgeeks.org/cpp/queue-push-and-queue-pop-in-cpp-stl/

⇱ queue push() and pop() in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 26 Sep, 2024

The std::queue::push() and std::queue::pop() functions in C++ STL are used to push the element at the back of the queue and remove the element from the front of the queue respectively. They are the member functions of the std::queue container defined inside the <queue> header file.

In this article we will learn how to use queue::push() and queue::pop() methods in C++.

queue::push()

The queue::push() function in C++ STL inserts an element at the back of the queue. This operation is called push operation hence the name push() method.

Syntax

q.push(val);

Parameters

  • val: Value to insert.

Return Value

  • This function does not return any value.

Example of queue::push() Method


Output
0 1 2 

Time Complexity: O(1)
Auxiliary Space: O(1)

queue::pop()

The queue::pop() function in C++ STL removes an element from the front of the queue. This operation is called pop operation hence the name. It works according to the FIFO (First-In-First-Out) order of deletion i.e. the element that was inserted first will be removed first.

Syntax

q.pop();

Parameters

  • This function does not take any parameter.

Return Value

  • This function does not return any value.

Example of queue::pop()


Output
1 2 

Time Complexity: O(1)
Auxiliary Space: O(1)

Difference Between queue::push() and queue::pop()

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

queue push() 

queue pop()

It is used to push a new element at the end of the queue.It is used to remove the front element from the queue.
Its syntax is -:
q.push (val);
Its syntax is -:
q.pop();
It takes one parameter that is the value to be inserted.It does not take any parameters.
Comment