C++ Standard Library C++ STL Library

C++ queue - push() Function



The C++ queue::push function is used to add a new element at the end of the queue. Addition of new element always occurs after its current last element and every addition results into increasing the container size by one.

Syntax

void push (const value_type& val);
void push (const value_type& val);
void push (value_type&& val);

Parameters

val Specify value which need to be added in the queue.

Return Value

None.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the queue::push function is used to add new elements in the queue called MyQueue.

#include <iostream>
#include <queue>
using namespace std;
 
int main (){
  queue<int> MyQueue;

  //add new elements in the queue
  MyQueue.push(10);
  MyQueue.push(20);
  MyQueue.push(30);
  MyQueue.push(40);
  MyQueue.push(50);


  cout<<"MyQueue contains:";
  while (!MyQueue.empty()) {
    cout<<" "<<MyQueue.front();
    MyQueue.pop();
  }
  return 0;
}

The output of the above code will be:

MyQueue contains: 10 20 30 40 50

❮ C++ <queue> Library

5