stl优先队列定义可以吗_C ++ STL | 用户定义的优先级队列比较器
stl優先隊列定義>可以嗎
In this article, we are going to see how to write your comparator function for priority queue in C++ STL using the lambda function. This is going to help you certainly to use priority queue more widely when you may have skipped thinking about how you can create a priority queue of your data type, or using your comparator.
在本文中,我們將看到如何使用lambda函數 在C ++ STL中為優先級隊列編寫比較器 函數 。 當您可能不考慮如何創建數據類型的優先級隊列或使用比較器時,這無疑將幫助您更廣泛地使用優先級隊列。
Syntax for default priority queue in C++ STL is:
C ++ STL中默認優先級隊列的語法為:
priority_queue<int> pq;By default the above priority queue works as the max heap, i.e., the maximum value from will come on the top and so on. So, if we pop and print we will have a sorted list in descending order.
默認情況下,上述優先級隊列用作最大堆,即from的最大值將排在頂部,依此類推。 因此,如果我們彈出并打印,我們將得到一個降序排列的列表。
使用priority_queue STL創建最小堆 (Create min heap using priority_queue STL)
We can use greater<int> class to define a min heap
我們可以使用Greater <int>類來定義最小堆
The syntax would be:
語法為:
priority_queue<int,vector<int>,greater<int>> pq;Where, vector<int> works as container and greater<int> as? comparator class,
其中, vector <int>用作容器,Greater <int>用作比較器類,
為優先級隊列定義自己的比較器 (Define your own comparator for priority queue)
You may have often come to a situation when you need to use a priority queue, but your datatype is something else that can't be compared by default (using '<' operator what is used by default). In such cases, we need to declare our comparator function.
您可能經常遇到需要使用優先級隊列的情況,但是您的數據類型是默認情況下無法比較的其他內容(默認情況下使用'<'運算符)。 在這種情況下,我們需要聲明比較器函數。
We can use the lambda function for that.
我們可以為此使用lambda函數 。
For example,
例如,
Say we need to compare the below object,
假設我們需要比較以下對象,
student{int rollint marks };And the comparison rule is if any two students have the same marks, then they would be sorted based on roll(whose roll comes first will have priority), otherwise, the student having more marks has more priority.
比較規則是,如果任何兩個學生的分數相同,則將基于擲骰進行排序(以擲骰優先),否則,得分更高的學生具有更高的優先級。
How would we define the comparator for the above?
我們如何定義以上比較器?
Below is the use of lambda function which will be our comparator
下面是lambda函數的使用,它將作為我們的比較器
The syntax is:
語法為:
auto it=[](student a, student b){//comparison logicif(a.marks>b.marks)return false;else if(a.marks<b.marks)return trueelse //when marks are sameif a.roll<b.rollreturn falseelsereturn true };The above is the lambda comparator function which takes as argument two data member and use the logic two compare, false means the current position is okay, that is no swap required, true means swap required.
上面是lambda比較器函數 ,該函數將兩個數據成員作為參數,并使用邏輯兩個比較, false表示當前位置可以,即不需要交換, true表示需要交換。
Now, the priority queue will be declared as:
現在,優先級隊列將聲明為:
priority_queue<student, vector<student>, decltype(it)> pq(it);Where it is our comparator function. One thing to notice that the comparator function is passed as constructor too.
它是我們的比較器功能。 需要注意的一件事是,比較器函數也作為構造函數傳遞。
So whenever you add an item to the priority queue,
因此,無論何時將項目添加到優先級隊列中,
It does necessary swaps according to our user-defined logic and places the items in an order.
它會根據用戶定義的邏輯進行必要的交換,并按順序放置項目。
Example:
例:
Say we have 5 students with below data,
假設我們有5位學生的數據如下,
Roll Marks 1 65 2 78 3 87 4 65 5 78So inserting the first student data in the priority queue.
因此,將第一個學生數據插入優先級隊列。
Since no other member.
由于沒有其他成員。
Priority queue will be now,
優先隊列現在是
1 65So inserting the second student data in the priority queue.
因此,將第二個學生數據插入優先級隊列。
Now as per our comparator there will be swap.
現在根據我們的比較器將進行交換。
And thus, the priority queue will be now,
因此,優先級隊列現在是
2 78 1 65So, inserting the third student data in the priority queue.
因此,在優先級隊列中插入第三個學生數據。
Now, as per our comparator, there will be a swap.
現在,根據我們的比較器,將進行交換。
And thus, the priority queue will be now,
因此,優先級隊列現在是
3 87 2 78 1 65So, inserting the fourth student data in the priority queue.
因此,在優先級隊列中插入第四個學生數據。
And thus as per our comparator, the priority queue will be now,
因此,根據我們的比較器,優先級隊列現在為
3 87 2 78 1 65 4 65So, inserting the fifth student data in the priority queue.
因此,在優先級隊列中插入第五個學生數據。
And thus as per our comparator, the priority queue will be now,
因此,根據我們的比較器,優先級隊列現在為
3 87 2 78 5 78 1 65 4 65So after popping we will get student list as,
因此,彈出后,我們將獲得學生名單,
3 87 2 78 5 78 1 65 4 65優先級隊列的用戶定義比較器的C ++實現 (C++ implementation for user-defined comparator for priority queue)
#include <bits/stdc++.h> using namespace std;class student { public:int roll;int marks;student(){roll = 0;marks = 0;} };void sort_students(vector<student> arr) {//comparator lambda functionauto comp = [](student a, student b) {//comparison logicif (a.marks > b.marks)return false;else if (a.marks < b.marks)return true;else { // when marks are sameif (a.roll < b.roll) {return false;}elsereturn true;}};priority_queue<student, vector<student>, decltype(comp)> pq(comp);for (auto& ij : arr) {pq.push(ij);}//printing the sorted listcout << "roll marks\n";while (!pq.empty()) {cout << pq.top().roll << " " << pq.top().marks << endl;pq.pop();} }int main() {int n;cout << "Enter number of students\n";cin >> n;vector<student> arr(n);cout << "Enter roll and marks for each student\n";for (int i = 0; i < n; i++) {int x, y;cin >> x >> y;arr[i].roll = x;arr[i].marks = y;}cout << "sorting students according to marks and roll no: \n";sort_students(arr);return 0; }Output:
輸出:
Enter number of students 5 Enter roll and marks for each student 1 65 2 78 3 87 4 65 5 78 sorting students according to marks and roll no: roll marks 3 87 2 78 5 78 1 65 4 65翻譯自: https://www.includehelp.com/stl/user-defined-comparator-for-priority-queue-in-cpp-stl.aspx
stl優先隊列定義>可以嗎
總結
以上是生活随笔為你收集整理的stl优先队列定义可以吗_C ++ STL | 用户定义的优先级队列比较器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx服务器配置安全维护,Nginx
- 下一篇: 如何打印出给定尺寸的方格_打印给定号码的