queue::emplace() in C++ STL

Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed.

queue::emplace()

This function is used to insert a new element into the queue container, the new element is added to the end of the queue.
Syntax :

queuename.emplace(value)
Parameters :
The element to be inserted into the queue
is passed as the parameter.

Result :
The parameter is added to the
forward list at the end.

Examples:

Input  : myqueue{1, 2, 3, 4, 5};
         myqueue.emplace(6);
Output : myqueue = 6, 1, 2, 3, 4, 5

Input  : myqueue{};
         myqueue.emplace(4);
Output : myqueue = 4

Errors and Exceptions
1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.
2. The parameter should be of the same type as that of the container otherwise, an error is thrown.

filter_none

edit
close

play_arrow

link
brightness_4
code

// INTEGER queue EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    queue<int> myqueue;
    myqueue.emplace(1);
    myqueue.emplace(2);
    myqueue.emplace(3);
    myqueue.emplace(4);
    myqueue.emplace(5);
    myqueue.emplace(6);
  
    // queue becomes 1, 2, 3, 4, 56
  
    while (!myqueue.empty()) {
        cout << ' ' << myqueue.front();
        myqueue.pop();
    }
   
    return 0;
      
}

chevron_right


Output:

1 2 3 4 5 6
filter_none

edit
close

play_arrow

link
brightness_4
code

// CHARACTER QUEUE EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    queue<char> myqueue;
    myqueue.emplace('k');
    myqueue.emplace('j');
    myqueue.emplace('y');
    myqueue.emplace('r');
    myqueue.emplace('y');
    myqueue.emplace('u');
  
    // queue becomes k, j, y, r, y, u
  
    while (!myqueue.empty()) {
        cout << ' ' << myqueue.front();
        myqueue.pop();
    }
   
    return 0;
      
}

chevron_right


Output:

k j y r y u
filter_none

edit
close

play_arrow

link
brightness_4
code

// STRING QUEUE EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    queue<string> myqueue;
    myqueue.emplace("This");
    myqueue.emplace("is");
    myqueue.emplace("a");
    myqueue.emplace("computer");
    myqueue.emplace("science");
    myqueue.emplace("portal");
  
    // queue becomes This, is, a, computer,
    //science, portal
  
    while (!myqueue.empty()) {
        cout << ' ' << myqueue.front();
        myqueue.pop();
    }
  
    return 0;
      
}

chevron_right


Output:

This is a computer science portal

Time Complexity : O(1)

Application : Input an empty queue and find the sum of the elements of the queue.

Input :  7, 6, 4, 2, 7, 8
Output : 34 

Algorithm
1. Insert elements into the queue using emplace() function.
2. Check if a queue is empty, if not add the front element to the sum variable and pop it.
3. Keep repeating this step until the queue becomes empty
4. Print the sum variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// application Of emplace() function
#include <queue>
#include <iostream>
using namespace std;
   
int main()
{
    // variable declaration
    int sum = 0;
      
    // queue declaration
    queue<int> myqueue{};
      
    // adding elements to the queue
    myqueue.emplace(7);
    myqueue.emplace(6);
    myqueue.emplace(4);
    myqueue.emplace(2);
    myqueue.emplace(7);
    myqueue.emplace(8);
  
    // queue becomes 7, 6, 4, 2, 7, 8
   
    // calculating the sum
    while (!myqueue.empty()) {
        sum = sum + myqueue.front();
        myqueue.pop();
    }
    cout<< sum;
}

chevron_right


Output

34


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.