English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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
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.
The following list of queue member types is provided, along with a brief description.
Member types | Description |
---|---|
value_type | It specifies the element type. |
container_type | It specifies the underlying container type. |
size_type | It specifies the size range of the elements. |
reference | It is a reference type of the container. |
const_reference | It is a reference type of constant container. |
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:
Function | Description |
---|---|
(constructor) | This function is used to construct the queue container. |
empty | This function is used to test if the queue is empty. If the queue is empty, this function returns true, otherwise it returns false. |
size | This function returns the number of elements in the queue. |
front | This function returns the first element. This element plays a very important role because all deletion operations are performed on the front element. |
back | This function returns the last element. This element plays a very important role because all insertion operations are performed on the back element. |
push | This function is used to insert a new element at the end. |
pop | This function is used to delete the first element. |
emplace | This function is used to insert a new element above the current back element in the queue. |
swap | This function is used to swap the contents of two containers referred to. |
relational operators | Non-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. |
#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