一、基本概念
priority_queue
是優先隊列,就像普通隊列一樣,只是隊列中的第一個元素是隊列中所有元素中最大的,算是C ++中的堆的一種實現,priority_queue
默認是最大堆。元素可以任意的順序插入,插入的時間複雜度爲O(logn)
。
創建int
類型的優先級隊列的語法:
priority_queue <int> pq;
二、成員方法
push
函數:在priority_queue
中插入一個元素,時間複雜度爲O(logn)
。pop
函數:從priority_queue
中刪除最上面的元素(最大元素),並將優先級隊列的大小減小1。top
函數:返回在priority_queue
頂部的元素,該元素是隊列中存在的最大元素。size
函數:返回priority_queue
的元素個數。empty
函數:返回true
和false
,如果priority_queue
爲空,則返回true
,否則返回false
。swap
函數:交換兩個priority_queue
的值。
#include <iostream>
#include <queue>
using namespace std;
void showpq(priority_queue <int> pq)
{
while (!pq.empty())
{
cout << ' ' << pq.top();
pq.pop();
}
cout << '\n';
}
int main()
{
priority_queue <int> pq1;
pq1.push(10); // inserts 10 to pq1 , now top = 10
pq1.push(30); // inserts 30 to pq1 , now top = 30
pq1.push(20); // inserts 20 to pq1 , now top = 30
pq1.push(50); // inserts 50 to pq1 , now top = 50
pq1.push(90); // inserts 90 to pq1 , now top = 90
cout << "The priority_queue pq1 is:";
showpq(pq1);
cout << "\npq1.size():" << pq1.size();
cout << "\npq1.top():" << pq1.top();
cout << "\npq1.pop()\n";
pq1.pop(); // remove 90 to pq1 , now top = 50
cout << "The priority_queue pq1 is:";
showpq(pq1);
priority_queue <int> pq2;
pq2.push(3);
pq2.push(5);
pq2.push(7);
pq2.push(9);
cout << "The priority_queue pq2 is:";
showpq(pq2);
pq1.swap(pq2);
cout << "after swap:" << endl;
cout << "The priority_queue pq1 is:";
showpq(pq1);
cout << "The priority_queue pq2 is:";
showpq(pq2);
system("pause");
return 0;
}
運行結果:
The priority_queue pq1 is: 90 50 30 20 10
pq1.size():5
pq1.top():90
pq1.pop()
The priority_queue pq1 is: 50 30 20 10
The priority_queue pq2 is: 9 7 5 3
after swap:
The priority_queue pq1 is: 9 7 5 3
The priority_queue pq2 is: 50 30 20 10
請按任意鍵繼續. . .
三、如何創建最小堆?
語法:
priority_queue<int, vector<int>, greater<int> > pq;
其中,greater
是STL
內建的關係運算類函數對象(也就是仿函數),並且是一個二元運算符。
template<class T> bool greater<T> //大於
#include <iostream>
#include <queue>
using namespace std;
template<typename T> void print_queue(T& q) {
while (!q.empty()) {
cout << q.top() << " ";
q.pop();
}
cout << '\n';
}
void showpq(priority_queue <int> pq)
{
while (!pq.empty())
{
cout << ' ' << pq.top();
pq.pop();
}
cout << '\n';
}
int main() {
priority_queue<int> pq1; // 最大堆
for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})
pq1.push(n);
print_queue(pq1);
priority_queue<int, vector<int>, greater<int> > pq2; // 最小堆
for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})
pq2.push(n);
print_queue(pq2);
system("pause");
return 0;
}
運行結果:
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
請按任意鍵繼續. . .
四、自定義數據類型的優先隊列用法
前面都是用的int
類型的優先隊列,如果是自定義數據類型,比如結構體和類,那麼怎麼使用優先隊列呢?
有一個Person
結構體,含有兩個變量Age
和Height
,定義如下:
struct Person{
int Age;
float Height;
}
在定義優先隊列的時候,priority_queue<Person> pq;
,程序並不知道該如何對Person
這種數據類型排序,就會報錯。這時就需要運算符重載或者寫仿函數來定義優先級,使得優先隊列知道如何存儲數據。對於自定義Person
類型。
- 運算符重載的方式
#include <iostream>
#include <queue>
using namespace std;
#define ROW 5
#define COL 2
struct Person {
int age;
float height;
// 初始化結構體變量
Person(int age, float height)
: age(age), height(height)
{
}
};
// 重載 operator<
bool operator<(const Person& p1, const Person& p2) {
return p1.height < p2.height;
}
int main()
{
priority_queue<Person> pq;
float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },
{ 20, 6 }, { 33, 6.1 }, { 23, 5.6 } };
for (int i = 0; i < ROW; ++i) {
// 使用Person的構造函數生成的臨時變量,壓到優先隊列pq中
pq.push(Person(arr[i][0], arr[i][1]));
}
while (!pq.empty()) {
Person p = pq.top();
pq.pop();
cout << p.age << " " << p.height << "\n";
}
system("pause");
return 0;
}
- 仿函數的方式
#include <iostream>
#include <queue>
using namespace std;
#define ROW 5
#define COL 2
struct Person {
int age;
float height;
// 初始化結構體變量
Person(int age, float height)
: age(age), height(height)
{
}
};
// 仿函數,裏面實現了Person類型的()運算符重載函數
struct CompareHeight {
bool operator()(Person const& p1, Person const& p2)
{
return p1.height < p2.height; // 升序
}
};
int main()
{
priority_queue<Person, vector<Person>, CompareHeight> pq;
float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },
{ 20, 6 }, { 33, 6.1 }, { 23, 5.6 } };
for (int i = 0; i < ROW; ++i) {
// 使用Person的構造函數生成的臨時變量,壓到優先隊列pq中
pq.push(Person(arr[i][0], arr[i][1]));
}
while (!pq.empty()) {
Person p = pq.top();
pq.pop();
cout << p.age << " " << p.height << "\n";
}
system("pause");
return 0;
}
運行結果:
33 6.1
20 6
23 5.6
30 5.5
25 5
請按任意鍵繼續. . .
五、參考文章
https://www.geeksforgeeks.org/priority-queue-in-cpp-stl/
https://en.cppreference.com/w/cpp/container/priority_queue
https://www.geeksforgeeks.org/stl-priority-queue-for-structure-or-class/