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

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of queue::push() and queue::pop() functions in C++ STL.

What is a queue in C++ STL?

Queue is a simple sequence or data structure defined in the C++ STL which does insertion and deletion of the data in FIFO(First In First Out) fashion. The data in a queue is stored in continuous manner. The elements are inserted at the end and removed from the starting of the queue. In C++ STL there is already a predefined template of queue, which inserts and removes the data in the similar fashion of a queue.

What is queue::push()?

queue::push() is an inbuilt function in C++ STL which is declared in  header file. queue::push() is used to push or insert a new element at the end or at the back of the queue container. push() accepts one parameter, that is the element which we want to push/insert in the associated queue container, also this function increases the size of container by 1.

This function further calls push_back() which helps in easy insertion of the element at the back of the queue.

Syntax

myqueue.push(type_t& value);

This function accepts one parameter the value which is of type_t that is the type of elements in the queue container.

Return value

This function returns nothing.

Example

Input: queue<int> myqueue = {10, 20 30, 40};
      myqueue.push(23);
Output:
      Elements in the queue are= 10 20 30 40 23

Example

 Live Demo

#include <iostream>
#include <queue>
using namespace std;
int main(){
   queue<int> Queue;
   for(int i=0 ;i<=5 ;i++){
      Queue.push(i);
   }
      cout<<"Elements in queue are : ";
   while (!Queue.empty()){
      cout << ' ' << Queue.front();
      Queue.pop();
   }
}

Output

If we run the above code it will generate the following output −

Elements in queue are : 0 1 2 3 4 5

What is queue::pop()?

queue::pop() is an inbuilt function in C++ STL which is declared in  header file. queue::pop() is used to push or delete an existing element from the beginning or start of the queue container. pop() accepts no parameter, and deletes the element from the beginning of the queue associated with the function and reduces the size of the queue container by 1.

Syntax

myqueue.pop();

This function accepts no parameters

Return value

This function returns nothing.

Example

Input: queue myqueue = {10, 20, 30, 40};
      myqueue.pop();
Output:
      Elements in the queue are= 20 30 40

Example

 Live Demo

#include <iostream>
#include <queue>
using namespace std;
int main(){
   queue<int> Queue;
   for(int i=0 ;i<=5 ;i++){
      Queue.push(i);
   }
   for(int i=0 ;i<5 ;i++){
      Queue.pop();
   }
   cout<<"Element left in queue is : ";
   while (!Queue.empty()){
      cout << ' ' << Queue.front();
      Queue.pop();
   }
}

Output

If we run the above code it will generate the following output −

Element left in queue is : 5
raja
Published on 06-Mar-2020 11:48:31
Advertisements