C++ Standard Library C++ STL Library

C++ queue - emplace() Function



The C++ queue::emplace function is used to insert new element at the end of the queue, after its current last element. Each insertion of element increases the queue container size by one.

Syntax

template <class... Args>
void emplace (Args&&... args);

Parameters

args Arguments forwarded to construct the new element.

Return Value

None.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the queue::emplace function is used to insert new element at the end of the queue MyQueue.

#include <iostream>
#include <queue>
using namespace std;
 
int main (){
  queue<int> MyQueue;
  
  //add new elements in the queue using emplace function
  MyQueue.emplace(10);
  MyQueue.emplace(20);
  MyQueue.emplace(30);
  MyQueue.emplace(40);
  MyQueue.emplace(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