คิวลำดับความสำคัญเป็นประเภทข้อมูลนามธรรมสำหรับการจัดเก็บคอลเลกชันขององค์ประกอบที่มีลำดับความสำคัญที่สนับสนุนการแทรกและการลบองค์ประกอบตามลำดับความสำคัญ นั่นคือองค์ประกอบที่มีลำดับความสำคัญเป็นอันดับแรกสามารถลบออกได้ตลอดเวลา คิวลำดับความสำคัญไม่จัดเก็บองค์ประกอบในลักษณะเชิงเส้นตามตำแหน่งขององค์ประกอบ เช่น ในกอง คิว รายการ ฯลฯ คิวลำดับความสำคัญ ADT (ประเภทข้อมูลนามธรรม) จะจัดเก็บองค์ประกอบตามลำดับความสำคัญ
คิวลำดับความสำคัญรองรับฟังก์ชันต่อไปนี้ −
ขนาด() − มันถูกใช้ในการคำนวณขนาดของลำดับความสำคัญในขณะที่ส่งกลับจำนวนขององค์ประกอบในนั้น
ว่างเปล่า() − คืนค่า จริง หากคิวลำดับความสำคัญว่างเปล่าและเป็นเท็จ มิฉะนั้น
แทรก(องค์ประกอบ) − ใช้เพื่อแทรกองค์ประกอบใหม่ลงในคิวลำดับความสำคัญ
นาที() − จะส่งคืนองค์ประกอบที่มีค่าคีย์ที่เกี่ยวข้องน้อยที่สุดและแสดงข้อความแสดงข้อผิดพลาดหาก Priority Queue ว่างเปล่า
removeMin() − จะลบองค์ประกอบที่อ้างอิงโดยฟังก์ชัน min()
ด้านล่างเป็นตารางที่แสดงผลกระทบของการดำเนินการกับคิวที่มีลำดับความสำคัญ

Start Step 1-> Declare function to display the elements in a Priority Queue void display(priority_queue <int> Pq) declare and set priority_queue <int> que = Pq Loop While (!que.empty()) call que.top() call que.pop() End Step 2-> In main() Create object of priority_queue <int> Pq Call push() to insert element in a priority queue as Pq.push(1) Call display(Pq) Call to check the size of a priority queue Pq.size() Call to display the top element of a priority queue Pq.top() Call to remove the elements of a priority queue Pq.pop() Call display(Pq) Stop
ตัวอย่าง
#include <iostream>
#include <queue>
using namespace std;
void display(priority_queue <int> Pq) {
priority_queue <int> que = Pq;
while (!que.empty()) {
cout << '\t' << que.top();
que.pop();
}
//cout << '\n';
}
int main () {
priority_queue <int> Pq;
Pq.push(1);
Pq.push(3);
Pq.push(5);
Pq.push(7);
Pq.push(9);
cout << "The priority queue is : ";
display(Pq);
cout << "\nPrioriy queue size using size() : " << Pq.size();
cout << "\nFirst element of priority queue using top(): " << Pq.top();
cout << "\nremoving element using pop() : ";
Pq.pop();
display(Pq);
return 0;
} ผลลัพธ์
The priority queue is : 9 7 5 3 1 Prioriy queue size using size() : 5 First element of priority queue using top(): 9 removing element using pop() : 7 5 3 1