[C ++ base] queue <queue> to use

As long as C ++ #include <queue> Queue class can be used, commonly used functions (in the usual order):

1. push

2. pop

3. size

4. empty

5. front

6. back

for example:

1. push

The tail is inserted in an element, such as:

 1 #include<queue>
 2 #include<string>
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 int main(){
 8     queue<string> q;
 9     q.push("Hello World!");
10     q.push("China");
11     cout << q.front() << endl;    
12 }

Output "Hello World!"

2. pop

The most forward position in the queue element is removed, a non-void return value of the function, such as:

1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 q.pop();
5 cout << q.front() << endl;  

Output China

3.size

Returns the number of elements in the queue, the return value of type unsigned int. Such as:

1 queue<string> q;
2 cout << q.size() << endl;
3 q.push("Hello World!");
4 q.push("China");
5 cout << q.size() << endl;

Two output lines, respectively, 0 and 2, i.e. the number of elements in the queue

4. empty

Whether the queue is empty, if returns true, if blank:

1 queue<string> q;
2 cout << q.empty() << endl;
3 q.push("Hello World!");
4 q.push("China");
5 cout << q.empty() << endl;    

Two output lines, respectively 1 and 0

5. front

The return value is the first element of the queue, i.e. the first element into the queue. Note that simply returns, and did not reject it out of the queue

1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 cout << q.front() << endl;
5 q.pop();
6 cout << q.front() << endl;    

Two output lines, respectively Hello World! And China. Only after using a pop, the first to enter the queue elements will be rejected

6. back

Returns the last element of the queue, that is, into the team's latest elements, such as:

1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 cout << q.back() << endl;

Output to China, because it was the last into the team. Back here just to return the last element, and not reject it

 

reference:

https://www.cnblogs.com/xuning/p/3321733.html

 

Guess you like

Origin www.cnblogs.com/cxc1357/p/12239134.html