The priority_queue () method using

Transfer from csdn article, only as study notes. Original link: https://blog.csdn.net/weixin_36888577/article/details/79937886

 

Common queue is a FIFO data structure, the end of the queue element is added, deleted from the queue head.

In the priority queue, the element is given priority. When accessing the element, the element with the highest priority is the first to be deleted. Priority queue with the most advanced, first-out (first in, largest out) the behavior characteristics.

 

The first to include the header file#include<queue> , he and queuedifferent is that we can customize where the priority of the data, so that a higher priority in the queue in front of priority from the team.

Queue priority queue has all the properties, including the basic operation of the queue, but this adds a sort on the basis of the internal, which is essentially a stack implemented.

Queues and the same basic operations:

  • top access head elements
  • Whether empty queue is empty
  • Returns the number of elements in the queue size
  • push element is inserted into the tail (and ordering)
  • emplace place a configuration and inserted into the queue element
  • pop pop the head elements
  • swap exchange content

Defined: The priority_queue <the Type, Container, Functional>
the Type is the data type, the type of container is Container (Container container arrays must be implemented, such as vector, deque and the like, but can not use the default is used inside list.STL Vector), Functional is the way of comparison.

When only need to use custom data types need to pass these three parameters, using basic data types, only incoming data types, the default is the top big heap.
Generally:

Copy the code
1 // ascending queue 
2 The priority_queue <int, Vector <int>, Greater <int>> Q; 
. 3 // descending queue 
. 4 The priority_queue <int, Vector <int>, less <int>> Q; 
. 5 
. 6 // Greater and less two functors std implemented (that is, the use of a class that looks like a function whose implementation is achieved in a class operator (), the behavior of this class have a similar function, is a functor class a)
Copy the code

 

1, an example of basic types of priority queues:

Copy the code
#Include. 1 <the iostream> 
 2 #include <Queue> 
 . 3 the using namespace STD; 
 . 4 int main () 
 . 5 { 
 . 6 // The default type is a large base for the stack top 
 . 7 priority_queue <int> A; 
 . 8 // equivalent priority_queue <int , Vector <int>, less <int>> A; 
 . 9      
10 // here must be a space, or become right-shift operator ↓↓ 
. 11 The priority_queue <int, Vector <int>, Greater <int>> C; / / this is a small top stack 
12 is The priority_queue <String> B; 
13 is 
14 for (int I = 0; I <. 5; I ++) 
15 { 
16 a.push (I); 
. 17 c.push (I); 
18 is} 
. 19 the while (! a.empty ()) 
20 is { 
21 is COUT << a.top () << '';
22         a.pop();
23     } 
24     cout << endl;
25 
26     while (!c.empty()) 
27     {
28         cout << c.top() << ' ';
29         c.pop();
30     }
31     cout << endl;
32 
33     b.push("abc");
34     b.push("abcd");
35     b.push("cbd");
36     while (!b.empty()) 
37     {
38         cout << b.top() << ' ';
39         b.pop();
40     } 
41     cout << endl;
42     return 0;
43 }
Copy the code

operation result:

1
2
3
4
4 3 2 1 0
0 1 2 3 4
cbd abcd abc
请按任意键继续. . .

2, made with the example of the priority queue element pair:

Rules: To compare the pair, the first comparison of the first element, the first relatively equal second.

Copy the code
 1 #include <iostream>
 2 #include <queue>
 3 #include <vector>
 4 using namespace std;
 5 int main() 
 6 {
 7     priority_queue<pair<int, int> > a;
 8     pair<int, int> b(1, 2);
 9     pair<int, int> c(1, 3);
10     pair<int, int> d(2, 5);
11     a.push(d);
12     a.push(c);
13     a.push(b);
14     while (!a.empty()) 
15     {
16         cout << a.top().first << ' ' << a.top().second << '\n';
17         a.pop();
18     }
19 }
Copy the code

operation result:

1
2
3
4
2 5
1 3
1 2
请按任意键继续. . .

3, an example of the priority queue elements made with custom type

Copy the code
#Include. 1 <the iostream> 
 2 #include <Queue> 
 . 3 the using namespace STD; 
 . 4 
 . 5 // method. 1 
 . 6 TMP1 struct // operator overloading < 
 . 7 { 
 . 8 int X; 
 . 9 TMP1 (int A) {A = X;} 
10 operator BOOL <(const & TMP1 A) const 
. 11 { 
12 is return X <AX; // large top stack 
13} 
14}; 
15 
16 // method 2 
. 17 TMP2 struct // override functor 
18 is { 
. 19 BOOL operator () ( A TMP1, TMP1 B) 
20 is { 
21 is return AX <BX; // large top stack 
22 is} 
23 is}; 
24 
25 int main () 
26 is { 
27 TMP1 A (. 1); 
28 TMP1 B (2); 
29 C TMP1 ( 3); 
30 The priority_queue <TMP1 > d;
31     d.push(b);
32     d.push(c);
33     d.push(a);
34     while (!d.empty()) 
35     {
36         cout << d.top().x << '\n';
37         d.pop();
38     }
39     cout << endl;
40 
41     priority_queue<tmp1, vector<tmp1>, tmp2> f;
42     f.push(b);
43     f.push(c);
44     f.push(a);
45     while (!f.empty()) 
46     {
47         cout << f.top().x << '\n';
48         f.pop();
49     }
50 }
Copy the code

operation result:

1
2
3
4
5
6
7
8
3
2
1
 
3
2
1
请按任意键继续. . .

Guess you like

Origin www.cnblogs.com/250101249-sxy/p/11301135.html