f64 的 BTreeMap

Eba*_*dta 3 rust

我想要一个排序数组,其中包含 f64 作为键和 f64 作为值。我需要通过找到正确的键来更新、删除和插入该数组。我需要获取前 1000 个排序条目,以及第一个条目。这些操作必须很快。

通过阅读文档,我认为 BTreeMap 对我有好处。

但是,当我尝试插入它时,我收到以下错误消息:

the trait bound `f64: Ord` is not satisfied
the trait `Ord` is not implemented for `f64`rustcE0277
Run Code Online (Sandbox Code Playgroud)

使用 Rust 执行此操作的推荐方法是什么?

我的代码:

use std::collections::BTreeMap;

pub struct MyStruct {
  pub map: BTreeMap<f64, f64>
}

impl MyStruct {
  pub fn new() -> MyStruct {
    MyStruct {
      map: BTreeMap::new()
    }
  }
}

fn main() {
    let mut my_struct = MyStruct::new();
    my_struct.map.insert(1.0, 2.0);
}
Run Code Online (Sandbox Code Playgroud)

Bal*_*Ben 5

我认为您可能正在寻找ordered_floatcrate 以及OrderedFloat(可以存储 NaN,在上面排序+inf)或NotNan(不能存储 NaN)结构,它们都是类浮点类型,即EqOrd(违反了 IEEE 标准)。