Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
communitycreator

What is queue.push() in C++?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The queue.push() function in C++ is used to insert an element in the back of the queue.

The image below shows the visual representation of the queue.push() function.

Visual representation of queue.push() function

In order to use the queue.push() function, we must include queue in the program, as shown below:

#include <queue>

Syntax

queue_name.push(element)
// where the queue_name is the name of the queue

Parameters

This function requires an element as a parameter.

Return value

The queue.push() function inserts the element sent as a parameter in the back of the queue.

Example

#include <iostream>
//header file
#include <queue>
using namespace std;

int main() {
  queue<int> queue1;
    queue1.push(1);
    queue1.push(3);
    queue1.push(5);
    queue1.push(2);
    queue1.push(0);
    //queue1 = 1->3->5->2->0
    cout<<"The queue1 after series of push : \n";
    while(queue1.empty()==false)
    {
      cout << queue1.front() << " ";
      queue1.pop();
    }  

}

RELATED TAGS

c++
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring