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 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优先队列定义>可以吗

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542244.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python编程求三角形面积公式_python编程 输入三角形的三条边,计算三角形的面积\...

展开全部# -*- coding: UTF-8 -*-# Filename : test.py# author by : www.runoob.coma float(input(输入三角62616964757a686964616fe59b9ee7ad9431333433633338形第一边长: ))b float(input(输入三角形第二边长: ))c float(input(输入三角形第三边长: ))# 计算半周长s (a …

ipfs分布式存储网络服务器系统,IPFS分布式存储是什么意思 分布式云存储服务器详解...

一直以来&#xff0c;数据的安全性&#xff0c;存储的隐私性都是用户很重视的方面。基于此&#xff0c;再加上现在媒体对于分布式存储的疯狂报道&#xff0c;分布式存储一词再度涌入了大家的视野之中&#xff0c;接下来IPFS新说就为大家详解一下有关IPFS分布式存储的知识。VIPF…

c# 插入树形数据#_C#数据类型能力问题 套装1

c# 插入树形数据#This section contains aptitude questions and answers on C# data types (set 1). 本节包含有关C&#xff03;数据类型(集合1)的能力问题和答案。 1) "int" is an alias of _________. System.Int16System.Int32System.Int64System.Byte Answer &…

python django框架怎么爬虫步骤_[Python爬虫]---Django视频教程

[↓↓↓资源简介↓↓↓]Django是一个开放源代码的Web应用框架&#xff0c;由Python写成。采用了MVC的框架模式&#xff0c;即模型M&#xff0c;视图V和控制器C。它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的&#xff0c;即是CMS(内容管理系统)软件…

小程序 || 语句_C ++开关语句| 查找输出程序| 套装1

小程序 || 语句Program 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){switch (printf("Hello World")) {case 0x09:cout << " India";break;case 0x0A:cout << " Australia";break;case 0x0B:co…

python爬虫与django_请问django和爬虫程序如何整合?

Django 模型是与数据库相关的&#xff0c;与数据库相关的代码一般写在 models.py 中&#xff0c;Django 支持 sqlite3, MySQL, PostgreSQL等数据库&#xff0c;只需要在settings.py中配置即可&#xff0c;不用更改models.py中的代码&#xff0c;丰富的API极大的方便了使用。本节…

Spark的枚举类型实例!scala的枚举。

Spark的枚举类型实例&#xff01;scala的枚举。Enumeration定义&#xff1a;[deploy] SparkSubmitAction { Value Value }Enumeration使用&#xff1a;appArgs. {SparkSubmitAction.> (appArgs)SparkSubmitAction.> (appArgs)SparkSubmitAction.> (appArgs) }转载于:…

c ++查找字符串_C ++类和对象| 查找输出程序| 套装5

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;class Sample {int X;int* PTR &X;public:void set(int x) const;void print();};void Sample::set(int x) const{*PTR x;}void Sample::print(){cout << *PTR - EOF <…

mysql8和5.7区别_mysql8.0与mysql5.7安全加密小差别

今天升级到了mysql8.0 做主从同步遇到下面问题2020-07-21T14:09:52.626718Z 13 [ERROR] [MY-010584] [Repl] Slave I/O for channel : error connecting to master slave_replication172.20.0.2:3306 - retry-time: 60 retries: 1 message: Authentication plugin caching_sha2…

c ++查找字符串_C ++类和对象| 查找输出程序| 套装3

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;class Sample {int X;public:void set(int x){X x;}void print(){cout << X << " ";}} A, B;int main(){A.set(10);B.set(20);A.print();B.print();return 0;…

时间轮

老早之前就听说时间轮算法特别高效&#xff0c;Linux内核都用的它&#xff0c;这两天抽空实现了遍……嗯&#xff0c;被差一bug搞死(~&#xffe3;▽&#xffe3;~) 啊哈 网上扣来的图&#xff0c;原理好懂&#xff1a;轮子里的每格代表一小段时间&#xff08;精度&#xff09;…

qc35 说明书_使用Bose QC35 2年的心得 | 迟而不迟的深度体验 | 文附佩戴效果照片...

小编注&#xff1a;此篇文章来自即可瓜分10万金币&#xff0c;周边好礼达标就有&#xff0c;邀新任务奖励无上限&#xff0c;点击查看活动详情创作立场声明&#xff1a;本文所测商品为自费购入&#xff0c;我会在文中点明。坚持来自内心的主观评测是起码的底线&#xff0c;不会…

threadgroup_Java ThreadGroup类的checkAccess()方法和示例

threadgroupThreadGroup类的checkAccess()方法 (ThreadGroup class checkAccess() method) checkAccess() method is available in java.lang package. checkAccess()方法在java.lang包中可用。 checkAccess() method is used to check whether the currently running thread h…

qt tab弹出特效_Nuke Studio 12(影视特效合成软件)中文版分享

Nuke 12是一款功能强大&#xff0c;世界知名的影视后期特效合成软件。NUKE是一个获得学院奖(Academy Award)的数码合成软件。已经经过10年的历练&#xff0c;为艺术家们提供了创造具有高质素的相片效果的图像的方法。NUKE无需专门的硬件平台&#xff0c;但却能为艺术家提供组合…

c ++ 链表_C ++程序查找两个单个链表的并集

c 链表Problem statement: Write a C program to find the union of two single linked lists. 问题陈述&#xff1a;编写一个C 程序来查找两个单个链表的并集。 Example: 例&#xff1a; Let the first linked list be:5->4->3->6->1->NULLLet the second l…

精华版线段树模板

哈哈哈&#xff0c;打了一上午。。。#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> using namespace std; typedef long long ll; ll a[10000010]; ll lazy[1000000]; …

【转】unity地形插件T4M使用帮助

unity的地形系统在手机游戏中因为效率问题基本无法使用&#xff0c;只能通过T4M这个地形插件来进行优化制作。下面大概讲解一下使用流程及方法。 先中U3D里面用自带的地形系统刷出想要的地形和贴图。贴图可以大概刷一下。后面要重新刷。 用导出脚本ExportTerrain.js导出地形为O…

ansys添加力矩_ANSYS软件中施加扭矩的方法

ANSYS软件中施加扭矩的方法胡意立&#xff0c;孙明礼&#xff0c;沈燕青&#xff0c;周佳杰&#xff0c;胡林强【摘要】在机械结构的有限元分析中&#xff0c;常会遇到施加扭矩的问题。文中探讨了在ANSYS软件中施加扭矩的一种方法&#xff0c;以在一个六棱柱一端施加扭矩为实例…

Python | 程序从列表中删除重复的元素

Example: 例&#xff1a; Input:list1: [10, 20, 10, 20, 30, 40, 30, 50]Output:List after removing duplicate elementslist2: [10, 20, 30, 40, 50]Logic: 逻辑&#xff1a; To implement the program is too easy, we have to append elements one by one to another…

Linux的简介与虚拟机的管理

Linux的简介&#xff1a; 严格的来讲&#xff0c;Linux不算是一个操作系统&#xff0c;只是一个Linux系统中的内核&#xff0c;Linux的全称是GUN/Linux&#xff0c;这才算是一个真正意义上的Linux系统。 Linux是一个多用户多任务的操作系统&#xff0c;拥有良好的用户界面&…