English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Queue (Queue)

In the field of computer science, we are committed to various programs. Each of them has its own domain and utility. According to the purpose and environment of program creation, we have a large number of data structures to choose from. One of them is 'queue'. Before discussing this data type, let's take a look at its syntax.

Syntax

template<class T, class Container = deque<T> > class queue;

This data structure is suitable for FIFO technology, where FIFO stands for First In First Out. The first inserted element will be extracted first, and so on. There is an element called 'front', which is the element located at the front position or the first position, and there is also an element called 'back', which is the element located at the last position. In a normal queue, elements are inserted at the end, and deletion starts from the front.

In the application area, the queue is implicitly a container adapter.

The container should support the following list of operations:

  • empty

  • size

  • push_back

  • pop_front

  • front

  • back

Template parameter

T: The parameter specifies the type of elements that the container adapter will retain.

Container: The parameter specifies the internal object of the container, where the queue elements are retained.

Member types

The following list of queue member types is provided, along with a brief description.

Member typesDescription
value_typeIt specifies the element type.
container_typeIt specifies the underlying container type.
size_typeIt specifies the size range of the elements.
referenceIt is a reference type of the container.
const_referenceIt is a reference type of constant container.

Function

With functions, objects or variables can be used in the programming field. The queue provides a large number of functions that can be used or embedded in the program. The same list is as follows:

FunctionDescription
(constructor)This function is used to construct the queue container.
emptyThis function is used to test if the queue is empty. If the queue is empty, this function returns true, otherwise it returns false.
sizeThis function returns the number of elements in the queue.
frontThis function returns the first element. This element plays a very important role because all deletion operations are performed on the front element.
backThis function returns the last element. This element plays a very important role because all insertion operations are performed on the back element.
pushThis function is used to insert a new element at the end.
popThis function is used to delete the first element.
emplaceThis function is used to insert a new element above the current back element in the queue.
swapThis function is used to swap the contents of two containers referred to.
relational operatorsNon-member functions specify the relational operators required by the queue.
uses allocator<queue>As the name suggests, non-member functions use an allocator for the queue.

Example: A simple program demonstrating the use of basic queue functions.

#include <iostream>
#include <queue>
using namespace std;
void showsg(queue<int> sg)
{
	queue<int> ss = sg;
	while (!ss.empty())
	{
		cout << '\t' << ss.front();
		ss.pop();
	}
	cout << '\n';
}
int main()
{
	queue<int> fquiz;
	fquiz.push(10);
	fquiz.push(20);
	fquiz.push(30);
	cout << "The queue fquiz is : ";
	showsg(fquiz);
	cout << "\nfquiz.size() : " << fquiz.size();
	cout << "\nfquiz.front() : " << fquiz.front();
	cout << "\nfquiz.back() : " << fquiz.back();
	cout << "\nfquiz.pop() : ";
	fquiz.pop();
	showsg(fquiz);
	return 0;
}

Output:

The queue fquiz is : 	10	20	30
fquiz.size() : 3
fquiz.front() : 10
fquiz.back() : 30
fquiz.pop() : 	20	30