Fly*_*FoX 3 iteration collections rust
我试图在2或更多的元组中迭代集合中的已排序元素.
如果我有Vec,我可以打电话
for window in my_vec.windows(2) {
// do something with window
}
Run Code Online (Sandbox Code Playgroud)
但是Vecs没有被隐式排序,这将是非常好的.我尝试使用a BTreeSet而不是a Vec,但我似乎无法调用windows它.
试图打电话时
for window in tree_set.iter().windows(2) {
// do something with window
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误
no method named `windows` found for type `std::collections::btree_set::Iter<'_, Card>` in the current scope
Run Code Online (Sandbox Code Playgroud)
Itertools提供的tuple_windows方法:
extern crate itertools;
use itertools::Itertools;
use std::collections::BTreeSet;
fn main() {
let items: BTreeSet<_> = vec![1, 3, 2].into_iter().collect();
for (a, b) in items.iter().tuple_windows() {
println!("{} < {}", a, b);
}
}
Run Code Online (Sandbox Code Playgroud)
注意,这windows是切片上的方法,而不是迭代器上的方法,它返回原始切片的子切片的迭代器.甲BTreeMap想必不能提供相同的迭代器接口,因为它不是建立在一个连续的数据大块的顶部; 会有一些值在内存中不会紧接着后续的值.
| 归档时间: |
|
| 查看次数: |
209 次 |
| 最近记录: |