[C++] How to use queue

How to use queue in C++

The queue template class (declared in the header file queue) is an adapter class. The queue template allows the underlying class (deque by default) to display a typical queue interface.
The quue template is more restrictive than deque. Not only does it not allow random access to queue elements , it does not even allow traversal of the queue . It restricts its use to the basic operations of defining a queue. You can add elements to the end of the queue, delete elements from the head of the queue, view the values ​​of the head and end of the queue, check the number of elements, and test whether the queue is empty.

1. The header file contains

#include <queue>

2. Define the queue object

queue<int> q1;

queue<double> q2;

3. Official initialization example

Insert picture description here

4. Overview of method functions

1. back()	返回最后一个元素

2. empty()	如果队列空则返回真

3. front()	返回第一个元素

4. pop()	删除第一个元素

5. push()	在末尾加入一个元素

6. size()	返回队列中元素的个数

Method function prototype and simple explanation

back
syntax:
TYPE &back();
back() returns a reference to the last element of the queue.

empty
Syntax:
bool empty();
empty () function returns true (true) if the queue is empty, false otherwise (false).

front
syntax:
TYPE &front();
front() returns a reference to the first element of the queue.

Pop
syntax: The
void pop();
pop() function deletes an element of the queue.

Push
syntax: The
void push( const TYPE &val );
push() function adds an element to the queue.

Size
syntax:
size_type size();
size() returns the number of elements in the queue.

Guess you like

Origin blog.csdn.net/phdongou/article/details/114247015