【发布时间】:2021-09-22 11:27:11
【问题描述】:
有谁知道是否可以在 Rust 中颠倒优先级队列中的顺序? 当我查看队列时,我希望接收到最低的 i32。但是,它似乎默认返回最高的 i32。
编辑
这是我正在使用的包:docs.rs/priority-queue/1.2.0/priority_queue
【问题讨论】:
-
嗨@MaxTorreSchau,你应该知道你正在使用哪个包。因为我不相信这是生锈的原生类型?是这个:docs.rs/priority-queue/1.2.0/priority_queue?
-
是的,没错!
-
那么不妨看看双优先队列:docs.rs/priority-queue/1.2.0/priority_queue/… 看来
peek_min将非常适合您的需求 -
如果您不需要即时更改顺序,另一种选择是在推送队列中的项目时反转优先级。 IE。
push (item, -prio)而不是push (item, prio)。 -
这能回答你的问题吗? How do I create a BinaryHeap that pops the smallest value, not the largest? 适用于
BinaryHeap,但该方法适用于任何依赖于PartialEq的数据结构。
标签: rust priority-queue