Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ STL में Priority_Queue का कार्यान्वयन

प्रायोरिटी कतार एक प्रकार के कंटेनर एडाप्टर हैं, जिसमें कतार का पहला तत्व कतार के सभी तत्वों में सबसे बड़ा होता है। प्रायोरिटी कतार में तत्व गैर-घटते क्रम में भी होते हैं। प्रायोरिटी कतार में उच्च प्राथमिकता वाला तत्व निम्न प्राथमिकता वाले तत्व से पहले सेवा प्राप्त करता है।

फंक्शन और विवरण:

  • pq.size() – प्रायोरिटी कतार का आकार लौटाता है।
  • pq.push() – प्रायोरिटी कतार में तत्व डालने के लिए उपयोग किया जाता है।
  • pq.pop() – प्रायोरिटी कतार से मान हटाता है।
  • pq.top() – प्रायोरिटी कतार के शीर्ष तत्व का संदर्भ लौटाता है।

उदाहरण कोड

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

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

    while (1) {
        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: ";
                cout << pq.size() << endl;
                break;
            case 2:
                cout << "Enter value to be inserted: ";
                cin >> i;
                pq.push(i);
                break;
            case 3:
                i = pq.top();
                if (!pq.empty()) {
                    pq.pop();
                    cout << i << " Deleted" << endl;
                } else {
                    cout << "Priority Queue is Empty" << endl;
                }
                break;
            case 4:
                cout << "Top Element of the Queue: ";
                cout << pq.top() << endl;
                break;
            case 5:
                exit(1);
                break;
            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: 4
Top Element of the Queue: 7
...
Enter your Choice: 5
Exit code: 1
  1. C++ STL में Set_Symmetric_Difference का कार्यान्वयन C++ STL में Set_Symmetric_Difference का कार्यान्वयन

    यह C++ प्रोग्राम set_symmetric_difference को लागू करने के लिए है। दो सेट का सिमेट्रिक डिफरेंस उन तत्वों द्वारा बनाया जाता है जो एक सेट में मौजूद होते हैं, लेकिन दूसरे में नहीं। सामान्य सेट ऑपरेशन हैं: Set Union Set Intersection Symmetric Set Difference या Exclusive-OR Set Difference या Subtraction

  1. C++ STL में Set_Intersection का कार्यान्वयन C++ STL में Set_Intersection का कार्यान्वयन

    दो सेट का इंटरसेक्शन केवल उन तत्वों द्वारा बनता है जो दोनों सेट में कॉमन होते हैं। फंक्शन द्वारा कॉपी किए गए तत्व हमेशा पहले सेट से उसी क्रम में आते हैं। दोनों सेट में तत्व पहले से क्रमबद्ध होने चाहिए। सामान्य सेट ऑपरेशन हैं: Set Union Set Intersection Symmetric Set Difference या Exclusive-OR Set

  1. C++ STL में Set_Difference का कार्यान्वयन C++ STL में Set_Difference का कार्यान्वयन

    दो सेट का डिफरेंस केवल उन तत्वों द्वारा बनता है जो पहले सेट में मौजूद होते हैं, दूसरे सेट में नहीं। फंक्शन द्वारा कॉपी किए गए तत्व हमेशा पहले सेट से उसी क्रम में आते हैं। दोनों सेट में तत्व पहले से क्रमबद्ध होने चाहिए। सामान्य सेट ऑपरेशन हैं: Set Union Set Intersection Symmetric Set Difference या