Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report