C++ STL queue (first in first out) queue detailed explanation

Part.I Attention

insert image description here

  • queue<T>The data of the container adapter is organized in a FIFO (Fisrt in First Out) manner, which can be imagined as a team of people waiting to do nucleic acid.
  • header file must be included#include <queue>
  • The header file queuenot only defines queue, but also defines priority_queue.
  • priority_queue<T>: Priority queue, which is queuedifferent from that in that we can customize the priority of the data in it, so that the priority queue is in front of the queue, and the priority is out of the queue. The default is descending order, the big root pile, and the largest element at the head of the team.
  • pop()Removes the first element in the queue, but returns nothing.

Part.II Funciton

Chap.I queue

queueThe object is very simple, it only has 9 functions, as shown in the figure below:

insert image description here

The following is a description of each function:

function explain
front() Returns a reference to the first element in queue. If queue is constant, a const reference is returned; if queue is empty, the return value is undefined.
back() Returns a reference to the last element in queue. If queue is constant, a const reference is returned; if queue is empty, the return value is undefined.
push(const T& obj) Appends a copy of the element to the end of the queue. This is push_back()done by calling member functions of the underlying container.
push(T&& obj) Adds elements to the end of the queue in a shifted fashion. push_back()This is done by calling member functions of the underlying container that have rvalue reference parameters .
pop() Removes the first element in queue, returns nothing.
size() Returns the number of elements in the queue.
empty() Returns true if there are no elements in the queue.
emplace() Invoking the constructor of T with the arguments passed to emplace() produces objects at the end of the queue.
swap(queue<T> &other_q) Exchange the elements in the current queue with the elements in the parameter queue. They need to contain elements of the same type. You can also call the global function template swap() to accomplish the same operation.

Chap.II priority_queue

priority_queueThe functions of objects and queueobjects are not the same, and its functions are somewhat similar to the functions of the stack, as follows:

function meaning
push(const T& obj) Puts a copy of obj in place in the container, usually including a sort operation.
push(T&& obj) Put obj in the appropriate position in the container, which usually includes a sort operation.
emplace(T constructor a rgs...) Constructs an Tobject at the appropriate position in the sequence by calling the constructor passed in the arguments. To maintain priority order, a sort operation is usually required.
top() Returns a reference to the first element in the priority queue.
pop() Remove the first element.
size() Returns the number of elements in the queue.
empty() Returns true if the queue is empty.
swap(priority_queue<T>& other) Swapped with the elements of the argument, the contained objects must be of the same type.

Part.III Code

The following is a simple example written by the author to learn this data structure.

Chap.I queue

The complete code is as follows:

#include <iostream>
#include <iomanip>
#include <queue>

using namespace std;

int main()
{
    
    
    int tmp=0;
    queue<int> que({
    
    0,1,2,3});  // queue<int> que;
    queue<int> que1({
    
    0,1,2,3}); 
    cout<< que.back() << setw(3) << que.front() <<endl;
    que.push(4);
    que.emplace(5);
    cout<< que.back() << setw(3) << que.front() << setw(3) << que.size() <<endl;
    que.pop();
    cout<< que.back() << setw(3) << que.front() << setw(3) << que.size() <<endl;
    que.swap(que1);
    cout<< que.back() << setw(3) << que.front() << setw(3) << que.size() <<endl;
    getchar();
    return 0;
}

The output is as follows:

3  0
5  0  6
5  1  5
3  0  4

Chap.II priority_queue

The complete code is as follows:

#include <iostream>
#include <iomanip>
#include <queue>
#include <vector>

using namespace std;

int main()
{
    
    
    int tmp=0;
    string st[]={
    
    "one","two","three","four","five","six","seven"};
    priority_queue<string, vector<string>, greater<string>> pq;  // priority_queue<string> que;
    priority_queue<string, vector<string>, greater<string>> pq1;
    for(auto& c:st) {
    
     pq.push(c); }
    pq1=pq;
    while(!pq.empty()) {
    
    
        cout<<pq.top()<<" ";
        pq.pop();
    }
    cout<<endl;
    cout<<setw(3)<<pq.size()<<setw(3)<<pq1.size()<<endl;
    pq.swap(pq1);
    cout<<setw(3)<<pq.size()<<setw(3)<<pq1.size()<<endl;
    getchar();
    return 0;
}

The output is as follows:

five four one seven six three two
  0  7
  7  0

Look priority_queueat the constructor:

priority_queue<T> p; //默认降序,大根堆,队头元素最大
priority_queue<T, vector<T>, less<T>> p; //相当于默认
priority_queue<T, vector<T>, greater<T>> p; //升序,最小值优先级队列,小根堆
priority_queue<string, vector<string>, greater<string>> pq
  • The first parameter is a function object used to sort the elements
  • The second parameter is a container providing the initial element
  • The third parameter specifies the sorting rules

Everything else is easier to understand.

Guess you like

Origin blog.csdn.net/Gou_Hailong/article/details/128381738