queue::emplace() in C++ STL
Last Updated :
21 Oct, 2020
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
Similar Reads
Queue in C++ STL In C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the dat
4 min read
How to Iterate a STL Queue in C++? A Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). Syntax: queue<datatype> queuename;Datatype: Queue can take any data type depending on the values, e.g. int, char, float, etc. The std: :queue container d
4 min read
queue push() and pop() in C++ STL The std::queue::push() and std::queue::pop() functions in C++ STL are used to push the element at the back of the queue and remove the element from the front of the queue respectively. They are the member functions of the std::queue container defined inside the <queue> header file.In this arti
2 min read
queue::front() and queue::back() in C++ STL Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::front() This function is used to reference the first or the oldest element of the queue container. This function can
3 min read
queue::empty() and queue::size() in C++ STL Queue is a type of container adaptor that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::empty() empty() function is used to check if the queue container is empty or not. SyntaxqueueName.empty()ParametersThis
4 min read
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 fu
3 min read
queue::swap() 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::swap() swap() fun
2 min read
Queue of Pairs in C++ STL with Examples Queue in STL are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement where elements are inserted at the back (end) and are deleted from the front. Queue of pair can be very efficient in designing complex data structures. The first element is referenced as âf
2 min read
Queue using Stacks Given a stack that supports push and pop operations, your task is to implement a queue using one or more instances of that stack along with its operations.Table of ContentBy Making Enqueue Operation CostlyBy Making Dequeue Operation Costly Queue Implementation Using One Stack and RecursionBy Making
11 min read
Implement thread-safe queue in C++ What is a Thread-safe Queue?A thread-safe queue is a data structure that is designed to provide thread safety for a concurrent environment. It is a data structure that allows multiple threads to access the same queue and enqueue and dequeue elements concurrently. The threads do not need to be synchr
3 min read