What is Queue in C++? And How to Implement it through function queue::emplace() in C++ STL?

Share Your Love

A queue in C++, also known as an abstract data type or a linear data structure, is a data structure that performs operations in a specific order. First in first out is the order (FIFO). The first piece added to the queue will be the first one withdrawn in a FIFO data structure.

queue::emplace()

This function is used to add a new element to the queue container, which is placed at the end of the queue.

Syntax OF Queue:

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{10, 20, 30, 40, 50};
         myqueue.emplace(60);
Output : myqueue = 10, 20, 30, 40, 50, 60

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

Join Our Community

Description for this block. Use this space for describing your block. Any text will do. Description for this block. You can use this space for describing your block.

Click To Join

Errors and Exceptions In Queue:

  1. It has a strong exception guarantee, which means that if an exception is thrown, no changes are made.
  2. The parameter must be of the same type as the container, else an error will be thrown.

Integer Queue Example:

// 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(10);
	myqueue.emplace(20);
	myqueue.emplace(30);
	myqueue.emplace(40);
	myqueue.emplace(50);
	myqueue.emplace(60);

	// queue becomes 1, 2, 3, 4, 5, 6

	while (!myqueue.empty())
	{
		cout << ' ' << myqueue.front();
		myqueue.pop();
	}
	return 0;
}

Output:

10 20 30 40 50 6
0

Application

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

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('a');
	myqueue.emplace('b');
	myqueue.emplace('c');
	myqueue.emplace('d');
	myqueue.emplace('e');
	myqueue.emplace('f');

	// queue becomes a, b, c, d, e, f

	while (!myqueue.empty())
	{
		cout << ' ' << myqueue.front();
		myqueue.pop();
	}
	return 0;
}

Output:

a b c d e f 

String Queue Example:

// 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).

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter