【发布时间】:2023-01-23 00:52:03
【问题描述】:
给定 n 元素的 HashMap 如何从 n-x 元素开始迭代。
元素的顺序无关紧要,我需要解决的唯一问题是从给定的键开始迭代。
例子:
let mut map: HashMap<&str, i32> = HashMap::new();
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);
map.insert("four", 4);
[...]
for (k, v) in map {
//how to start iteration from third item and not the first one
}
试图用谷歌搜索它,但到目前为止还没有找到例子。
【问题讨论】:
-
HashMap 是无序的,“从给定键开始”迭代没有意义。它可以从无到有覆盖整个地图。
-
您要维护插入顺序还是排序顺序?