priority_queue priority queue

priority_queue The priority queue, whose bottom layer is implemented with a heap. In a priority queue, the head element must be the one with the highest priority in the current queue.

  In the priority queue, there is no front() function and back() function, and only the top() function can be used to access the first element of the queue (also known as the top element of the heap), that is, the element with the highest priority.

1. The basic data types referred to here are int type, double type, char type and other data types that can be used directly. The priority setting of the priority queue for them is generally the higher priority of the larger number, so the first element of the queue is the priority queue. The one with the largest element in it (if it is of type char, it is the largest lexicographically).

Priority queue:

//The definitions of the following two priority queues are equivalent
    priority_queue<int> q;
    priority_queue<int,vector<int>,less<int> >;//There is a space after

The second parameter ( vector ) is a container to carry the underlying data structure heap, and the third parameter ( less ) is a comparison class. Less indicates that a larger number has a higher priority, while greater indicates a smaller number. high .

If you want the priority queue to always put the smallest element at the head of the queue, just define it as follows:

priority_queue<int,vector<int>,greater<int> >q;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325885027&siteId=291194637