Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the knowledge points of C++ priority queue usage?

Shulou Source: shulou.com Published: 2022-06-03 05:06:27 09月30日 Update

C++ priority queue usage knowledge points, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem to find a simpler and easier way.

C++ priority queue usage detailed explanation

Priority queues are also a type of queue data structure. Its operation is not limited to the queue first-in first-out, can be logically (according to the maximum or minimum value of the queue).

A normal queue is a first-in, first-out data structure in which elements are appended at the end of the queue and deleted from the head.

In the priority queue, elements are given priority. When elements are accessed, the element with the highest priority is deleted first. Priority queues have the behavior of first in, largest out.

First of all, we must include the header file #include, which is different from queue in that we can customize the priority of the data, so that the priority is higher in the queue, priority out of the queue.

Priority queues have all the characteristics of queues, including the basic operations of queues, but add an internal sort on this basis, which is essentially a heap implementation.

The basic operations of queues are the same:

top Access Team Head Element

empty Is the queue empty

size Returns the number of elements in the queue

push insert element to end of queue (and sort)

emplace constructs an element in place and inserts it into the queue

pop team leader element

swap content

Definition: priority_queue

Type is the data type, Container is the container type (Container must be a container implemented in an array, such as vector,deque, etc., but not a list). STL default is vector), Functional is the way to compare.

These three parameters need to be passed in when a custom data type is needed. When a basic data type is used, only the data type needs to be passed in. The default is the large top heap.

Generally:

//ascending queue priority_queue q;//descending queue priority_queue q; //greater and less are two imitations of std implementations (i.e., making the use of a class look like a function). Its implementation is to implement an operator() in the class, this class has a function-like behavior, that is, an imitation function class)

1. Examples of basic types of priority queues:

#include#include using namespace std;int main() { //For base type default is large top heap priority_queue a; //Equivalent to priority_queue a; //There must be spaces here, otherwise it becomes right shift operator priority_queue c; //This is small top heap priority_queue b; for (int i = 0; i < 5; i++) { a.push(i); c.push(i); } while (! a.empty()) { cout

Tags: Queues elements types that is data functions priorities knowledge caches examples methods results problems first-out runs knowledge points highest advanced content basic operations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Shulou Technology MariaDB Apple