当前位置:网站首页>priority_queue元素为指针时,重载运算符失效
priority_queue元素为指针时,重载运算符失效
2022-08-04 01:36:00 【萧国浪子】
使用priority_queue构造最大最小堆时,发现priority_queue中元素为指针时,std::greater/std::less函数并不能调用到自定义数据的重载运算符,排序结果是根据指针地址大小计算的,从而导致最大最小堆失效。
#include <iostream>
#include <vector>
#include <queue>
void log(const char* str)
{
std::cout << str;
}
void log(const int v)
{
std::cout << "" << +v << " ";
}
struct Stuent
{
int score = 0;
bool operator >(const Stuent& right) const
{
return this->score > right.score;
}
};
int main()
{
//构造最小堆
std::priority_queue<Stuent*, std::vector<Stuent*>, std::greater<Stuent*>> minHeap;
for (auto i : { 1,3,5,9,8,6,2,7,4,0 })
{
Stuent* stu=new Stuent;
stu->score = i * 10;
minHeap.push(stu);
}
log("最小堆:\n");
while (!minHeap.empty())
{
auto stu = minHeap.top();
log(stu->score);
printf("%lld\n", (uint64_t)stu);
minHeap.pop();
delete stu;
stu = nullptr;
}
getchar();
return 0;
}
失效的最小堆,可以看到是按指针地址排序的。
如何解决呢?
实现比较函数:
constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left > _Right;
}
测试代码如下:
#include <iostream>
#include <vector>
#include <queue>
void log(const char* str)
{
std::cout << str;
}
void log(const int v)
{
std::cout << "" << +v << " ";
}
struct Stuent
{
int score = 0;
bool operator >(const Stuent& right) const
{
return this->score > right.score;
}
};
struct StuentCmp
{
bool operator()(const Stuent* left, const Stuent* right) const
{
return left->score > right->score;
}
};
int main()
{
//构造最小堆
std::priority_queue<Stuent*, std::vector<Stuent*>, StuentCmp> minHeap;
for (auto i : { 1,3,5,9,8,6,2,7,4,0 })
{
Stuent* stu=new Stuent;
stu->score = i * 10;
minHeap.push(stu);
}
log("最小堆:\n");
while (!minHeap.empty())
{
auto stu = minHeap.top();
log(stu->score);
minHeap.pop();
delete stu;
stu = nullptr;
}
getchar();
return 0;
}
参考资料:https://en.cppreference.com/w/cpp/utility/functional/greater
边栏推荐
- 可变字符串
- nodejs安装及环境配置
- KunlunBase 1.0 发布了!
- Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
- 哎,又跟HR在小群吵了一架!
- nodejs installation and environment configuration
- [store mall project 01] environment preparation and testing
- 静态文件快速建站
- Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
- GNSS【0】- 专题
猜你喜欢
2022年上半年各大厂Android面试题整理及答案解析(持续更新中......)
Flink jdbc connector 源码改造sink之 clickhouse多节点轮询写与性能分析
静态/动态代理模式
Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
特征值与特征向量
js中常用的几种遍历处理数据的方法梳理
如何用C语言代码实现商品管理系统开发
[store mall project 01] environment preparation and testing
【store商城项目01】环境准备以及测试
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
随机推荐
简单的线性表的顺序表示实现,以及线性表的链式表示和实现、带头节点的单向链表,C语言简单实现一些基本功能
Use nodejs switch version (no need to uninstall and reinstall)
typescript54 - generic constraints
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
计算首屏时间
flask框架初学-06-对数据库的增删改查
typescript52 - simplify generic function calls
【虚拟户生态平台】虚拟化平台安装时遇到的坑
Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
nodejs 安装多版本 版本切换
快速入门EasyX图形编程
贴纸拼词 —— 记忆化搜索 / 状压DP
JS 从零教你手写节流throttle
即席查询——Presto
实例040:逆序列表
Thinkphp commonly used techniques
MySQL回表指的是什么
如何用C语言代码实现商品管理系统开发
splice随机添加和删除的写法