Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ สำหรับการใช้งาน Priority Queue ใน STL

Priority Queue (คิวลำดับความสำคัญ) เป็นประเภทของคอนเทนเนอร์แดปเตอร์ ที่องค์ประกอบแรก (top) จะเป็นค่ามากที่สุดของทุกองค์ประกอบในคิว องค์ประกอบจัดเรียงแบบไม่ลดลง (non-decreasing) องค์ประกอบที่มีความสำคัญสูงจะถูกให้บริการก่อนองค์ประกอบที่มีความสำคัญต่ำกว่า

ฟังก์ชันและคำอธิบาย

  • pq.size() – คืนขนาดของ priority queue
  • pq.push() – ใช้แทรกองค์ประกอบเข้า priority queue (ต้นฉบับเขียนผิดเป็น insert)
  • pq.pop() – ลบองค์ประกอบบนสุดออกจาก priority queue (ต้นฉบับเขียนผิดเป็น delete)
  • pq.top() – คืนอ้างอิงไปยังองค์ประกอบบนสุดของ priority queue
  • pq.empty() – ตรวจสอบว่าว่างหรือไม่ (สำคัญสำหรับความปลอดภัย)

โค้ดตัวอย่าง (โปรแกรมโต้ตอบแบบเมนู - แก้ไขข้อผิดพลาดแล้ว)

#include <iostream>
#include <queue>
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    priority_queue<int> pq;
    int c, i;

    while (true) {
        cout << "1.Size of the Priority Queue" << endl;
        cout << "2.Insert Element into the Priority Queue" << endl;
        cout << "3.Delete Element from the Priority Queue" << endl;
        cout << "4.Top Element of the Priority Queue" << endl;
        cout << "5.Exit" << endl;
        cout << "Enter your Choice: ";
        cin >> c;

        switch(c) {
            case 1:
                cout << "Size of the Queue: " << pq.size() << endl;
                break;
            case 2:
                cout << "Enter value to be inserted: ";
                cin >> i;
                pq.push(i);
                break;
            case 3:
                if (!pq.empty()) {
                    i = pq.top();
                    pq.pop();
                    cout << i << " Deleted" << endl;
                } else {
                    cout << "Priority Queue is Empty" << endl;
                }
                break;
            case 4:
                if (!pq.empty())
                    cout << "Top Element of the Queue: " << pq.top() << endl;
                else
                    cout << "Priority Queue is Empty" << endl;
                break;
            case 5:
                exit(0);
            default:
                cout << "Wrong Choice" << endl;
        }
    }
    return 0;
}

ตัวอย่างผลลัพธ์

1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Size of the Queue: 0
Enter your Choice: 2
Enter value to be inserted: 2
Enter your Choice: 2
Enter value to be inserted: 1
Enter your Choice: 2
Enter value to be inserted: 7
Enter your Choice: 2
Enter value to be inserted: 6
Enter your Choice: 4
Top Element of the Queue: 7
Enter your Choice: 5
Exit code: 0

หมายเหตุสำคัญ: โค้ดต้นฉบับมีข้อบกพร่องหลายประการ: 1. Case 1 ขาด break ทำให้ตกไป case 2 ตลอด 2. ใช้ pq.insert() และ pq.delete() ซึ่งไม่มีฟังก์ชันเหล่านี้ (ถูกต้องคือ push() และ pop()) 3. เรียก pq.top() ก่อนตรวจสอบ empty() โค้ดด้านบนได้แก้ไขข้อบกพร่องทั้งหมดแล้ว

ค่าเริ่มต้น: Priority queue เรียงจากมากไปน้อย (max-heap) หากต้องการ min-heap ให้ใช้ priority_queue<int, vector<int>, greater<int>>