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 roll
int 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 logic
if(a.marks>b.marks)
return false;
else if(a.marks<b.marks)
return true
else //when marks are same
if a.roll<b.roll
return false
else
return 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 78
So inserting the first student data in the priority queue.
因此,将第一个学生数据插入优先级队列。
Since no other member.
由于没有其他成员。
Priority queue will be now,
优先队列现在是
1 65
So 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 65
So, 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 65
So, 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 65
So, 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 65
So 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 function
auto comp = [](student a, student b) {
//comparison logic
if (a.marks > b.marks)
return false;
else if (a.marks < b.marks)
return true;
else { // when marks are same
if (a.roll < b.roll) {
return false;
}
else
return true;
}
};
priority_queue<student, vector<student>, decltype(comp)> pq(comp);
for (auto& ij : arr) {
pq.push(ij);
}
//printing the sorted list
cout << "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优先队列定义>可以吗