【发布时间】:2019-02-02 02:10:11
【问题描述】:
我可以使用std::collections::BinaryHeap 使用pop 以最大 到最小 顺序迭代结构的集合,但我的目标是迭代集合从小到大。
我已经成功逆转了Ord 的实现:
impl Ord for Item {
fn cmp(&self, other: &Self) -> Ordering {
match self.offset {
b if b > other.offset => Ordering::Less,
b if b < other.offset => Ordering::Greater,
b if b == other.offset => Ordering::Equal,
_ => Ordering::Equal, // ?not sure why compiler needs this
}
}
}
现在BinaryHeap 返回Items 从最小到最大。看到这不是预期的 API,这是一个不正确或容易出错的模式吗?
我意识到LinkedList 会给我pop_front 方法,但我需要在插入时对列表进行排序。这是更好的解决方案吗?
【问题讨论】:
-
这不是预期的 API — 你到底是什么意思?
-
我的意思是文档 ion 说接下来会出现最伟大的项目,所以当我实现这个反转时,对于(代码的)新读者来说,这将是意想不到的行为
标签: sorting rust binary-heap