Detailed Explanation of C++ Priority Queue (priority_queue)

Table of contents

1. Definition

2. Element access in the priority queue

3. Common functions of priority queue

Fourth, the priority of the elements in the priority queue


         Priority queue (priority_queue), the underlying data structure is a heap (heap) , so as to ensure that the first element of the queue must be the highest priority among all elements of the current queue. We can also add (push) elements to the priority team at any time, and the first element of the team is still the highest priority.

1. Definition

        Header file: #include<queue>

        The definition is written in the same way as other STL containers. Type can be any basic data type or container, and Container is a container type (here must be a container implemented with an array, such as vector, deque, but list cannot be used. The default in STL is vector ), Functional is the comparison method (ascending queue: large top heap; descending queue: small top heap):

priority_queue<Type,Container,Functional> name;

2. Element access in the priority queue

        Ordinary queue is a first-in first-out data structure, elements are added at the end of the queue and deleted at the head of the queue.

        In the priority queue, when an element is accessed, the element is given a priority, and the element with a higher priority is deleted first and dequeued first.

        The difference between the priority queue and the normal queue is that we can define the priority of the elements in the queue, and control the dequeue order through the priority. The priority queue has no front() function and back() function , and can only access the first element of the queue (that is, the top element of the heap) through the top() function:

#include<iostream>
#include<queue>
using namespace std;
int main()
{
    priority_queue<int> pot;
    pot.push(2);
    pot.push(6);
    pot.push(3);
    cout << pot.top() << endl;
    return 0;
} 

Output result: 6

3. Common functions of priority queue

        It is roughly the same as the basic operation of the queue:

  • push() inserts elements to the end of the queue and sorts them, the time complexity is O(logN)
  • top() accesses the first element of the queue, and the time complexity is O(1). Before using it, it should be judged whether the priority queue is empty
  • pop pops up the first element of the queue, and the time complexity is O(logN). Before using it, it should be judged whether the priority queue is empty
  • empty() to determine whether the queue is empty, the time complexity is O(1)
  • size() returns the number of elements in the queue, and the time complexity is O(1)
  • emplace() constructs an element in place and inserts it into the queue 
  • swap() swap content
#include<iostream>
#include<queue>
using namespace std;
int main()
{
    priority_queue<int> dsp;
    //判断优先队列是否为空
    cout << dsp.empty() << endl;
    dsp.push(2);
    dsp.push(4);
    dsp.push(1);
    dsp.push(5);
    //输出队首元素
    cout << dsp.top() << endl;
    //弹出队首元素,再输出队顶元素
    dsp.pop();
    cout << dsp.top() << endl;
    //输出优先队列元素个数
    cout << dsp.size() << endl;
}

 Output result:

Fourth, the priority of the elements in the priority queue

        For basic data types such as int, double, char, etc., their priority is generally that the priority of the larger number is higher (if it is char type, it is based on the lexicographical size):

priority_queue<int> dsp;
priority_queue<int, vector<int>, less<int> > dsp;
priority_queue<int, vector<int>, greater<int> > dsp;

        The first is the common definition method. Two parameters are added to the latter two. vector<> is the container used to carry the underlying heap structure; less<> and greater<> are the comparison classes of the first parameter, respectively Indicates that the larger the number, the greater the priority (large top heap), and the smaller the number, the higher the priority (small top heap).

Guess you like

Origin blog.csdn.net/m0_62573214/article/details/129212357