当前位置:网站首页>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
边栏推荐
- [store mall project 01] environment preparation and testing
- Analysis: What makes the Nomad Bridge hack unique
- nodejs+express realizes the access to the database mysql and displays the data on the page
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
- 【虚拟户生态平台】虚拟化平台安装时遇到的坑
- What warehouse management problems can WMS warehouse management system solve in the electronics industry?
- LeetCode third topic (the Longest Substring Without Repeating Characters) trilogy # 3: two optimization
- 网络带宽监控,带宽监控工具哪个好
- 工程制图复习题(带答案)
- FeatureNotFound( bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested:
猜你喜欢

LeetCode third topic (the Longest Substring Without Repeating Characters) trilogy # 3: two optimization

一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包

企业虚拟偶像产生了实质性的价值效益

flask框架初学-06-对数据库的增删改查

【store商城项目01】环境准备以及测试

HBuilderX的下载安装和创建/运行项目

Slipper - virtual point, shortest path

initramfs详解----添加硬盘驱动并访问磁盘

nodejs+npm的安装与配置
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
随机推荐
特征值与特征向量
Demand analysis of MES management system in electronic assembly industry
js中常用的几种遍历处理数据的方法梳理
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
typescript50 - type specification between cross types and interfaces
网络带宽监控,带宽监控工具哪个好
this巩固训练,从两道执行题加深理解闭包与箭头函数中的this
多渠道打包
天地图坐标系转高德坐标系 WGS84转GCJ02
initramfs详解----添加硬盘驱动并访问磁盘
Observability:你所需要知道的关于 Syslog 的一些知识
Web3 security risks daunting?How should we respond?
What warehouse management problems can WMS warehouse management system solve in the electronics industry?
Jmeter cross-platform operation CSV files
idea中diagram使用
【无标题】
typescript56 - generic interface
静态文件快速建站
Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
nodejs+express realizes the access to the database mysql and displays the data on the page