Open In App

queue::emplace() in C++ STL

Last Updated : 21 Oct, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 = 1, 2, 3, 4, 5, 6

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.

CPP




// 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, 5, 6
 
    while (!myqueue.empty())
    {
        cout << ' ' << myqueue.front();
        myqueue.pop();
    }
    return 0;
}


Output: 

1 2 3 4 5 6

CPP




// 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;
}


Output: 

k j y r y u

CPP




// 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;
}


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. 

CPP




// 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;
}


Output 

34


Next Article

Similar Reads

Article Tags :
Practice Tags :
three90RightbarBannerImg
  翻译: