详解Rust标准库:HashSet
## 查看本地官方文档
安装rust
后运行
rustup doc
查看The Standard Library
即可获取标准库内容
std::collections::hash_set::HashSet定义
HashSet
是一种集合数据结构,它只存储唯一的元素。它主要用于检查元素是否存在于集合中,或者对元素进行去重操作,两个键相等,则它们的哈希值必须相等
HashSet
定义
// 默认哈希器为RandomState
pub struct HashSet<T, S = RandomState> {
// 负责管理哈希表的存储和操作。它包含了哈希表的具体实现细节,如存储桶(buckets)、哈希函数、冲突解决策略
base: base::HashSet<T, S>,
}
方法
with_capacity
:创建一个具有指定初始容量的HashSet
,可以预先分配足够的空间,避免在插入元素时频繁重新分配内存
use std::collections::HashSet;
fn main() {
let hash_set: HashSet<u8> = HashSet::with_capacity(20);
println!("{}", hash_set.capacity())
// 28
}
capacity
:返回HashSet
当前的容量,即它可以容纳的元素数量,不包括已经被占用的空间用于处理哈希冲突等情况
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::with_capacity(5);
hash_set.insert(1);
hash_set.insert(2);
println!("Capacity: {}", hash_set.capacity());
// Capacity: 7
}
iter
:返回一个不可变迭代器,用于遍历HashSet
中的元素。元素的顺序是由哈希值和哈希表的内部结构决定的,不是按照插入顺序
use std::collections::HashSet;
fn main() {
let hash_set = HashSet::from([1, 2, 3]);
for element in hash_set.iter() {
println!("{}", element);
}
// 3
// 1
// 2
}
len
:返回HashSet
中元素的数量
use std::collections::HashSet;
fn main() {
let hash_set = HashSet::from([1, 2, 3]);
println!("Length: {}", hash_set.len());
// Length: 3
}
is_empty
:判断HashSet
是否为空
use std::collections::HashSet;
fn main() {
let hash_set: HashSet<i32> = HashSet::new();
println!("Is empty? {}", hash_set.is_empty());
// Is empty? true
let non_empty_set = HashSet::from([1]);
println!("Is non - empty set empty? {}", non_empty_set.is_empty());
// s non - empty set empty? false
}
drain
:移除HashSet
中的所有元素,并返回一个迭代器,允许在移除元素的同时对其进行处理
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::from([1, 2, 3]);
let drained: HashSet<i32> = hash_set.drain().collect();
println!("Drained set: {:?}", drained);
// Drained set: {1, 2, 3}
println!("Original set after drain: {:?}", hash_set);
// Original set after drain: {}
}
retain
:保留满足给定谓词的元素,删除不满足的元素
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::from([1, 2, 3, 4, 5]);
hash_set.retain(|&x| x % 2 == 0);
println!("Retained set: {:?}", hash_set);
// Retained set: {4, 2}
}
clear
:移除HashSet
中的所有元素,使其变为空集
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::from([1, 2, 3]);
hash_set.clear();
println!("Cleared set: {:?}", hash_set);
// Cleared set: {}
}
with_hasher
:使用指定的哈希器(hasher)创建一个HashSet
。哈希器用于计算元素的哈希值,可以用于自定义哈希策略
use std::collections::HashSet;
use std::hash::RandomState;
fn main() {
let s = RandomState::new();
let mut set = HashSet::with_hasher(s);
set.insert(2);
println!("{:?}", set);
// {2}
}
with_capacity_and_hasher
:同时指定初始容量和哈希器来创建一个HashSet
use std::collections::HashSet;
use std::hash::RandomState;
fn main() {
let s = RandomState::new();
let set: HashSet<u8> = HashSet::with_capacity_and_hasher(10, s);
let hasher: &RandomState = set.hasher();
println!("Capacity: {}", set.capacity());
// Capacity: 14
println!("hasher: {:?}", hasher);
// hasher: RandomState { .. }
}
hasher
:返回HashSet
当前使用的哈希器
use std::collections::HashSet;
fn main() {
let hash_set: HashSet<u8> = HashSet::new();
let hasher = hash_set.hasher();
println!("{:?}", hasher);
// RandomState { .. }
}
reserve
:增加HashSet
的容量,确保它可以容纳至少指定数量的额外元素
use std::collections::HashSet;
fn main() {
let mut hash_set: HashSet<u8> = HashSet::new();
hash_set.reserve(10);
println!("{}", hash_set.capacity());
// 14
}
try_reserve
:尝试增加HashSet
的容量,如果无法增加则返回false
。这是一种更安全的容量增加方式,避免可能的内存分配错误
use std::collections::HashSet;
fn main() {
let mut hash_set: HashSet<u8> = HashSet::new();
if hash_set.try_reserve(10).is_ok() {
println!("Reserved successfully");
// Reserved successfully
} else {
println!("Failed to reserve");
}
println!("Capacity: {}", hash_set.capacity());
// 14
}
shrink_to_fit
:将HashSet
的容量调整为与当前元素数量相匹配,释放多余的内存
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::with_capacity(10);
hash_set.insert(1);
hash_set.insert(2);
println!("{}", hash_set.capacity());
// 14
hash_set.shrink_to_fit();
println!("{}", hash_set.capacity());
// 3
}
shrink_to
:将HashSet
的容量缩减为不超过指定的最小值。如果当前容量小于指定值,则不进行任何操作
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::with_capacity(10);
hash_set.insert(1);
hash_set.insert(2);
println!("{}", hash_set.capacity());
// 14
hash_set.shrink_to(5);
println!("{}", hash_set.capacity());
// 7
}
difference
:返回一个迭代器,包含在当前HashSet
中但不在另一个HashSet
中的元素
use std::collections::HashSet;
fn main() {
let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);
for x in a.difference(&b) {
println!("{x}"); // 1
}
// 在a但不在b中的元素
let diff: HashSet<_> = a.difference(&b).collect();
println!("{:?}", diff);
// {1}
// 在b但不在a中的元素
let diff: HashSet<_> = b.difference(&a).collect();
println!("{:?}", diff);
// {4}
}
symmetric_difference
:返回一个迭代器,包含在当前HashSet
或另一个HashSet
中,但不同时在两个集合中的元素
use std::collections::HashSet;
fn main() {
let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);
for x in a.symmetric_difference(&b) {
println!("{x}"); // 1 4
}
let diff1: HashSet<_> = a.symmetric_difference(&b).collect();
let diff2: HashSet<_> = b.symmetric_difference(&a).collect();
println!("diff1: {:?}", diff1);
// diff1: {1, 4}
println!("diff2: {:?}", diff2);
// diff2: {4, 1}
}
intersection
:返回一个迭代器,包含同时在当前HashSet
和另一个HashSet
中的元素,即交集
use std::collections::HashSet;
fn main() {
let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);
for x in a.intersection(&b) {
println!("{x}"); // 2 3
}
let intersection: HashSet<_> = a.intersection(&b).collect();
println!("{:?}", intersection);
// {2, 3}
}
union
:返回一个联合迭代器,包含在当前HashSet
或另一个HashSet
中的所有元素
use std::collections::HashSet;
fn main() {
let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);
for x in a.union(&b) {
println!("{x}"); // 1 2 3 4
}
let union: HashSet<_> = a.union(&b).collect();
println!("Union: {:?}", union);
// Union: {1, 2, 3, 4}
}
contains
:检查HashSet
是否包含指定的元素
use std::collections::HashSet;
fn main() {
let hash_set = HashSet::from([1, 2, 3]);
println!("Contains 2? {}", hash_set.contains(&2));
// Contains 2? true
}
get
:返回一个指向HashSet
中指定元素的引用,如果元素不存在则返回None
use std::collections::HashSet;
fn main() {
let hash_set = HashSet::from([1, 2, 3]);
if let Some(element) = hash_set.get(&2) {
println!("Element found: {}", element);
// Element found: 2
}
}
is_disjoint
:检查当前HashSet
与另一个HashSet
是否没有共同的元素
use std::collections::HashSet;
fn main() {
let set1 = HashSet::from([1, 2, 3]);
let set2 = HashSet::from([4, 5, 6]);
println!("Is disjoint? {}", set1.is_disjoint(&set2));
// Is disjoint? true
}
is_subset
::检查当前HashSet
是否是另一个HashSet
的子集,即当前集合中的所有元素都在另一个集合中
use std::collections::HashSet;
fn main() {
let set1 = HashSet::from([1, 2]);
let set2 = HashSet::from([1, 2, 3]);
println!("Is subset? {}", set1.is_subset(&set2));
// Is subset? true
}
is_superset
:检查当前HashSet
是否是另一个HashSet
的超集,即另一个集合中的所有元素都在当前集合中
use std::collections::HashSet;
fn main() {
let set1 = HashSet::from([1, 2, 3]);
let set2 = HashSet::from([1, 2]);
println!("Is superset? {}", set1.is_superset(&set2));
// Is superset? true
}
insert
:向HashSet
中插入一个元素。如果元素已经存在,则不进行任何操作
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::new();
hash_set.insert(1);
println!("{:?}", hash_set);
// {1}
}
replace
:向HashSet
中插入一个元素,替换掉已有的相同元素。如果元素不存在,则插入新元素
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::from([1]);
hash_set.replace(2);
println!("Replaced set: {:?}", hash_set);
// Replaced set: {2, 1}
}
remove
:从HashSet
中移除指定的元素。如果元素不存在,则不进行任何操作
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::from([1, 2, 3]);
hash_set.remove(&2);
println!("Removed set: {:?}", hash_set);
// Removed set: {1, 3}
}
take
:从HashSet
中移除并返回指定的元素,如果元素不存在则返回None
use std::collections::HashSet;
fn main() {
let mut hash_set = HashSet::from([1, 2, 3]);
if let Some(element) = hash_set.take(&2) {
println!("Taken element: {}", element);
// Taken element: 2
}
println!("Set after take: {:?}", hash_set);
// Set after take: {1, 3}
}
原文地址:https://blog.csdn.net/weixin_62799021/article/details/143452793
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!