Priority_queue of C++STL common operations
Introduction:
#include<queue>
Queue: https://blog.csdn.net/qq_45985728/article/details/112624175
Priority queue: The elements in the queue will be given priority. When accessing the element, the element with the highest priority will be accessed first, which means that the priority queue has the characteristics of the highest priority first.
It can be said that it is a perfect combination of queue and sorting, which can not only store data, but also sort according to our prescribed rules.
The time complexity is O(logn).
1. Structure
#include<iostream>
#include<queue>
using namespace std;
int init[5] = {
10,6,8,2,4 };
int main() {
priority_queue<int> q1; //构造了一个以int为元素数据类型的优先队列
for (int i = 0; i < 5; ++i)
q1.push(init[i]); //将数组init中的元素依次入队
while (!q1.empty()) {
cout << q1.top() << " "; q1.pop(); //依次输出并出队
}
return 0;
}
We can see that the results of 10, 6, 8, 2, 4 into the team in turn, and then out of the team are 10, 8, 6, 4, 2.
The priority queue does have a sorting effect.
2. Commonly used functions
priority_queue<int> q1;
q1.push(1); //入队
q1.size(); //返回优先队列的元素个数
q1.top(); //返回对头元素
q1.pop(); //对头元素出队
q1.empty() //bool型,判断是否队空
3. Other structures
#include<iostream>
#include<queue>
using namespace std;
int init[5] = {
10,6,8,2,4 };
int main() {
priority_queue<int, vector<int>, less<int>> q1; //构造以int为数据类型较大元素优先的队列
priority_queue<int, vector<int>, greater<int>> q2; //构造以int为数据类型较小元素优先的队列
for (int i = 0; i < 5; ++i){
q1.push(init[i]);
q2.push(init[i]);
}
cout << "q1:";
while (!q1.empty()) {
cout << q1.top() << " "; q1.pop(); //依次输出并出队
}
cout << endl << "q2:";
while (!q2.empty()) {
cout << q2.top() << " "; q2.pop(); //依次输出并出队
}
return 0;
}
priority_queue<int, vector, less> q1; non-ascending order, in the first part we actually omitted the latter two parameters, because the priority queue is the default larger element with higher priority.
priority_queue<int, vector, greater> q2; non-descending order.
4. Custom type
#include<iostream>
#include<queue>
using namespace std;
struct Node {
int data1;
int data2;
};
struct cmp1 {
bool operator()(Node a, Node b) {
return a.data1 > b.data1;
}
}; //按data1从小到大的规则排序
struct cmp2 {
bool operator()(Node a, Node b) {
return a.data2 > b.data2;
}
}; //按data2从小到大的规则排序
struct cmp3 {
bool operator()(Node a, Node b) {
return a.data1 + a.data2 > b.data1 + b.data2;
}
}; //按data1和data2的和从小到大的规则排序
int main() {
priority_queue<Node, vector<Node>, cmp1> q1; //构造按cmp1为规则的优先队列
priority_queue<Node, vector<Node>, cmp2> q2; //构造按cmp2为规则的优先队列
priority_queue<Node, vector<Node>, cmp3> q3; //构造按cmp3为规则的优先队列
Node a;
for (int i = 1; i <= 5; ++i) {
cin >> a.data1 >> a.data2; //输入5个Node并且分别进入q1,q2和q3
q1.push(a);
q2.push(a);
q3.push(a);
}
cout << "q1:" << endl;
while (!q1.empty()) {
cout << q1.top().data1 << " " << q1.top().data2 << endl; q1.pop();
}
cout << "q2:" << endl;
while (!q2.empty()) {
cout << q2.top().data1 << " " << q2.top().data2 << endl; q2.pop();
}
cout << "q3:" << endl;
while (!q3.empty()) {
cout << q3.top().data1 << " " << q3.top().data2 << endl; q3.pop();
}
//依次输出q1,q2和q3中元素并且出队
return 0;
}
From the running results, you can learn from three different cmp rules.
If you find any problems, please correct me!
Please leave a message if you don’t understand!