STL प्रायोरिटी क्यू मैक्सहीप का कार्यान्वयन है।
यह स्ट्रक्चर के लिए STL प्रायोरिटी क्यू का एक C++ प्रोग्राम है।
एल्गोरिथम
Begin Define a structure of type student. Initialize variables in student structure. Define another structure of type comparemarks Overload the variables of student structure in comapremarks structure. Use priority queue with structure. Insert some elements in priority queue using student structure. While the queue is not empty do Print the elements. End.
उदाहरण कोड
#include <iostream> #include <queue> using namespace std; #define ROW 6 #define COL 3 struct student { //स्ट्रक्चर student को परिभाषित करना int roll,marks; student(int roll, int marks) : roll(roll), marks(marks) { } }; struct comparemarks{ //स्ट्रक्चर comparemarks को परिभाषित करना bool operator()(student const& s1, student const& s2) //स्ट्रक्चर student के ऑपरेटर्स को ओवरलोड करना { return s1.marks < s2.marks; } }; int main() { priority_queue<student, vector<student>, comparemarks> M; // प्रायोरिटी क्यू का उपयोग करना। हमें प्रायोरिटी क्यू का उपयोग करने के लिए इस प्रकार के सिंटैक्स का उपयोग करना होगा। int a[ROW][COL] = {{15, 50}, {16, 60}, {18,70}, {14, 80}, {12, 90}, {20, 100}}; for (int i = 0; i < ROW; ++i) { M.push(student(a[i][0], a[i][1])); //क्यू में वेरिएबल्स डालना } cout<<"priority queue for structure ::"<<endl; while (!M.empty()) { student s = M.top(); M.pop(); cout << s.roll << " " << s.marks << "\n"; //मान प्रिंट करना } return 0; }
आउटपुट
priority queue for structure :: 20 100 12 90 14 80 18 70 16 60 15 50