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.
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.
// 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; } |
Output:
1 2 3 4 5 6
// 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
// 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 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
Recommended Posts:
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Find the maximum cost path from the bottom-left corner to the top-right corner
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples
- Go vs C++
- Trie Data Structure using smart pointer and OOP in C++
- Modular Exponentiation of Complex Numbers
- std::hash class in C++ STL
- History of C++
- What are Forward declarations in C++
- __builtin_inf() functions of GCC compiler
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
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.



