详解Rust标准库:BTreeSet
查看本地官方文档
安装rust
后运行
rustup doc
查看The Standard Library
即可获取标准库内容
std::collections::BTreeSet定义
B树第一个元素最小,最后一个元素最大
BTreeSet
定义
pub struct BTreeSet<T, A: Allocator + Clone = Global> {// 使用BTreeMap来实现BTreeSet// SetValZST是一个特殊的值类型,它实际上是一个空的结构体(ZST,Zero - Sized Type)// A是分配器类型,用于管理BTreeMap内部的内存分配map: BTreeMap<T, SetValZST, A>,
}
方法
range
:返回一个迭代器,包含指定范围内的元素
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::new();set.insert(1);set.insert(3);set.insert(5);for element in set.range(1..4) {println!("{}", element); // 1 3}
}
difference
:返回一个迭代器,包含在当前BTreeSet
中但不在另一个BTreeSet
中的元素
use std::collections::BTreeSet;
fn main() {let set1 = BTreeSet::from([1, 2, 3]);let set2 = BTreeSet::from([3, 4, 5]);let difference: BTreeSet<_> = set1.difference(&set2).collect();println!("Difference: {:?}", difference);// Difference: {1, 2}
}
symmetric_difference
:返回一个迭代器,包含在当前BTreeSet
或另一个BTreeSet
中,但不同时在两个集合中的元素
use std::collections::BTreeSet;
fn main() {let set1 = BTreeSet::from([1, 2, 3]);let set2 = BTreeSet::from([3, 4, 5]);let symmetric_difference: BTreeSet<_> = set1.symmetric_difference(&set2).collect();println!("Symmetric difference: {:?}", symmetric_difference);// Symmetric difference: {1, 2, 4, 5}
}
intersection
:返回一个迭代器,包含同时在当前BTreeSet
和另一个BTreeSet
中的元素
use std::collections::BTreeSet;
fn main() {let set1 = BTreeSet::from([1, 2, 3]);let set2 = BTreeSet::from([3, 4, 5]);let intersection: BTreeSet<_> = set1.intersection(&set2).collect();println!("Intersection: {:?}", intersection);// Intersection: {3}
}
union
:返回一个联合迭代器,包含在当前BTreeSet
或另一个BTreeSet
中的所有元素,默认升序排列
use std::collections::BTreeSet;
fn main() {let set1 = BTreeSet::from([1, 2, 3]);let set2 = BTreeSet::from([3, 4, 5]);let union: BTreeSet<_> = set1.union(&set2).collect();println!("Union: {:?}", union);// Union: {1, 2, 3, 4, 5}
}
clear
:移除BTreeSet
中的所有元素,使其变为空集
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::from([1, 2, 3]);set.clear();println!("Cleared set: {:?}", set);// Cleared set: {}
}
contains
:检查BTreeSet
是否包含指定的元素
use std::collections::BTreeSet;
fn main() {let set = BTreeSet::from([1, 2, 3]);println!("Contains 2? {}", set.contains(&2));// Contains 2? true
}
get
:返回一个指向BTreeSet
中指定元素的引用,如果元素不存在则返回None
use std::collections::BTreeSet;
fn main() {let set = BTreeSet::from([1, 2, 3]);if let Some(element) = set.get(&2) {println!("Element found: {}", element);// Element found: 2}
}
is_disjoint
:检查当前BTreeSet
与另一个BTreeSet
是否没有共同的元素
use std::collections::BTreeSet;
fn main() {let set1 = BTreeSet::from([1, 2, 3]);let set2 = BTreeSet::from([4, 5, 6]);println!("Is disjoint? {}", set1.is_disjoint(&set2));// Is disjoint? true
}
is_subset
:检查当前BTreeSet
是否是另一个BTreeSet
的子集,即当前集合中的所有元素都在另一个集合中
use std::collections::BTreeSet;
fn main() {let set1 = BTreeSet::from([1, 2]);let set2 = BTreeSet::from([1, 2, 3]);println!("Is subset? {}", set1.is_subset(&set2));// Is subset? true
}
is_superset
:检查当前BTreeSet
是否是另一个BTreeSet
的超集,即另一个集合中的所有元素都在当前集合中
use std::collections::BTreeSet;
fn main() {let set1 = BTreeSet::from([1, 2, 3]);let set2 = BTreeSet::from([1, 2]);println!("Is superset? {}", set1.is_superset(&set2));// Is superset? true
}
first
:返回BTreeSet
中的第一个元素(最小元素)的引用,如果集合为空则返回None
use std::collections::BTreeSet;
fn main() {let set = BTreeSet::from([1, 2, 3]);if let Some(first) = set.first() {println!("First element: {}", first);// First element: 1}
}
last
:返回BTreeSet
中的最后一个元素(最大元素)的引用,如果集合为空则返回None
use std::collections::BTreeSet;
fn main() {let set = BTreeSet::from([1, 2, 3]);if let Some(last) = set.last() {println!("Last element: {}", last);// Last element: 3}
}
pop_first
:移除并返回BTreeSet
中的第一个元素(最小元素),如果集合为空则返回None
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::from([1, 2, 3]);if let Some(pop) = set.pop_first() {println!("Popped first element: {}", pop);// Popped first element: 1}println!("Set after pop first: {:?}", set);// Set after pop first: {2, 3}
}
pop_last
:移除并返回BTreeSet
中的最后一个元素(最大元素),如果集合为空则返回None
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::from([1, 2, 3]);if let Some(pop) = set.pop_last() {println!("Popped last element: {}", pop);// Popped last element: 3}println!("Set after pop last: {:?}", set);// Set after pop last: {1, 2}
}
insert
:向BTreeSet
中插入一个元素。如果元素已经存在,则不进行任何操作
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::new();set.insert(1);println!("{}", set.contains(&1));// true
}
replace
:向BTreeSet
中插入一个元素,替换掉已有的相同元素。如果元素不存在,则插入新元素
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::from([1]);set.replace(2);println!("Replaced set: {:?}", set);// Replaced set: {1, 2}
}
remove
:从BTreeSet
中移除指定的元素。如果元素不存在,则不进行任何操作
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::from([1, 2, 3]);set.remove(&2);println!("Removed set: {:?}", set);// Removed set: {1, 3}
}
take
:从BTreeSet
中移除(获取并消耗)并返回指定的元素,如果元素不存在则返回None
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::from([1, 2, 3]);if let Some(element) = set.take(&2) {println!("Taken element: {}", element);// Taken element: 2}println!("Set after take: {:?}", set);// Set after take: {1, 3}
}
retain
:保留满足给定谓词的元素,删除不满足的元素
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::from([1, 2, 3, 4, 5]);set.retain(|&x| x % 2 == 0);println!("Retained set: {:?}", set);// Retained set: {2, 4}
}
append
:将另一个BTreeSet
中的所有元素添加到当前BTreeSet
中
use std::collections::BTreeSet;
fn main() {let mut a = BTreeSet::new();a.insert(1);a.insert(2);a.insert(3);let mut b = BTreeSet::new();b.insert(3);b.insert(4);b.insert(5);a.append(&mut b);println!("{:?}", a);// {1, 2, 3, 4, 5}
}
split_off
:将BTreeSet
从指定元素处分割成两个部分,返回后半部分,原集合变为前半部分
use std::collections::BTreeSet;
fn main() {let mut set = BTreeSet::from([1, 2, 3, 4]);let second_half = set.split_off(&3);println!("First half: {:?}", set);// First half: {1, 2}println!("Second half: {:?}", second_half);// Second half: {3, 4}
}
iter
:返回一个不可变迭代器,用于遍历BTreeSet
中的元素。元素是按照从小到大的顺序遍历的
use std::collections::BTreeSet;
fn main() {let set = BTreeSet::from([1, 2, 3]);for element in set.iter() {println!("{}", element); // 1 2 3}
}
len
:返回BTreeSet
中元素的数量
use std::collections::BTreeSet;
fn main() {let set = BTreeSet::from([1, 2, 3]);println!("Length: {}", set.len());// Length: 3
}
is_empty
:判断BTreeSet
是否为空
use std::collections::BTreeSet;
fn main() {let set: BTreeSet<i32> = BTreeSet::new();println!("Is empty? {}", set.is_empty());// Is empty? truelet non_empty_set = BTreeSet::from([1]);println!("Is non - empty set empty? {}", non_empty_set.is_empty());// Is non - empty set empty? false
}