Detailed usage of c++ priority_queue

Articles from csdn, just as learning notes. Original Link: https://blog.csdn.net/weixin_36888577/article/details/7993788...

Articles from csdn, just as learning notes. Original Link: https://blog.csdn.net/weixin_36888577/article/details/79937886

A common queue is a first-in-first-out data structure in which elements are appended to the end of the queue and deleted from the header.

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

The first thing to include is the header file #include<queue>, which differs from queue in that we can customize the priority of the data so that the higher priority ranks first in the queue and the higher priority ranks first.

A priority queue has all the characteristics of a queue, including the basic operations of the queue, but adds an internal sort to it, which is essentially a heap implementation.

Same basic operation as queue:

  • top Access Queue Header Element
  • Is the empty queue empty
  • size returns the number of elements in the queue
  • push inserts elements to the end of the queue (and sorts)
  • emplace constructs an element in place and inserts a queue
  • Pop pop-up queue head element
  • swap exchange content

Definition: priority_ Queue<Type, Container, Functional>
Type is the data type, Container is the container type (Container must be an array of containers, such as vector, dequeue, etc., but not a list. STL uses vector by default, Functional is the way of comparison.

When you need to use a custom data type, you need to pass in these three parameters. When you use the basic data type, you only need to pass in the data type, which defaults to the large top heap.
Generally:

//Ascending Queue, Small Top Heap priority_queue <int,vector<int>,greater<int> > q; //Descending Queue, Large Top Heap priority_queue <int,vector<int>,less<int> >q; //Greter and less are two imitation functions implemented by std (that is, to make the use of a class look like a function. Their implementation is to implement an operator() in a class, which behaves like a function, and is a function-like class).

1. Examples of basic Type-First queues:

#include<iostream> #include <queue> using namespace std; int main() { //Default is large top heap for base type priority_queue<int> a; //Equivalent to priority_ Queue<int, vector<int>, less<int> > a; // There must be a space here, or it will become the right shift operator_ priority_queue<int, vector<int>, greater<int> > c; //This is the small top heap priority_queue<string> b; for (int i = 0; i < 5; i++) { a.push(i); c.push(i); } while (!a.empty()) { cout << a.top() << ' '; a.pop(); } cout << endl; while (!c.empty()) { cout << c.top() << ' '; c.pop(); } cout << endl; b.push("abc"); b.push("abcd"); b.push("cbd"); while (!b.empty()) { cout << b.top() << ' '; b.pop(); } cout << endl; return 0; }

Run result:

4 3 2 1 0 0 1 2 3 4 cbd abcd abc Press any key to continue. . .

2. Example of using pair as a priority queue element:

Rule: pair comparison, compares the first element first and the second equally.

#include <iostream> #include <queue> #include <vector> using namespace std; int main() { priority_queue<pair<int, int> > a; pair<int, int> b(1, 2); pair<int, int> c(1, 3); pair<int, int> d(2, 5); a.push(d); a.push(c); a.push(b); while (!a.empty()) { cout << a.top().first << ' ' << a.top().second << '\n'; a.pop(); } }

Run result:

2 5 1 3 1 2 Press any key to continue. . .

3. Example of using custom types as priority queue elements

#include <iostream> #include <queue> using namespace std; //Method 1 struct tmp1 //Operator overload < { int x; tmp1(int a) bool operator<(const tmp1& a) const { return x < a.x; //Top heap } }; //Method 2 struct tmp2 //Override Parody Function { bool operator() (tmp1 a, tmp1 b) { return a.x < b.x; //Top heap } }; int main() { tmp1 a(1); tmp1 b(2); tmp1 c(3); priority_queue<tmp1> d; d.push(b); d.push(c); d.push(a); while (!d.empty()) { cout << d.top().x << '\n'; d.pop(); } cout << endl; priority_queue<tmp1, vector<tmp1>, tmp2> f; f.push(b); f.push(c); f.push(a); while (!f.empty()) { cout << f.top().x << '\n'; f.pop(); } }

Run result:

3 2 1 3 2 1 Press any key to continue. . .

8 November 2021, 20:36 | Views: 9351

Add new comment

For adding a comment, please log in
or create account

0 comments