rust btreeset
时间: 2023-08-24 15:06:13 浏览: 104
`BTreeSet` is a data structure in Rust that represents a set of unique values stored in a sorted binary tree. It provides efficient operations for inserting, removing, and checking for the presence of elements in the set.
One advantage of `BTreeSet` over other set implementations in Rust is its ability to maintain the order of elements in the set. This can be useful in situations where you need to iterate over the elements in a particular order, such as when working with sorted data.
Here's an example of how to use `BTreeSet`:
```rust
use std::collections::BTreeSet;
let mut set = BTreeSet::new();
// Add some elements to the set
set.insert(5);
set.insert(2);
set.insert(8);
// Check if an element is in the set
assert!(set.contains(&5));
// Remove an element from the set
set.remove(&2);
// Iterate over the elements in the set (in sorted order)
for element in set.iter() {
println!("{}", element);
}
```
This will output:
```
5
8
```
阅读全文