Struct std::collections::BTreeSet
1.0.0 · source · pub struct BTreeSet<T, A = Global>where
A: Allocator + Clone,{ /* private fields */ }
Expand description
基于 B 树的有序 set。
有关此集合的性能优缺点的详细讨论,请参见 BTreeMap
文档。
以某种方式修改项目是一种逻辑错误,即该项目相对于任何其他项目的排序 (由 Ord
trait 确定) 会在其位于集合中时发生变化。
通常只有通过 Cell
,RefCell
,二进制状态,I/O 或不安全代码才能实现此操作。
此类逻辑错误导致的行为未指定,但会封装到观察到逻辑错误的 BTreeSet
中,并且不会导致未定义的行为。
这可能包括 panics、不正确的结果、中止、内存泄漏和未中止。
BTreeSet::iter
返回的迭代器按顺序生成它们的项,并且每个返回的项目都采用最坏情况的对数和摊销常数时间。
Examples
use std::collections::BTreeSet;
// 通过类型推断,我们可以省略显式类型签名 (在本示例中为 `BTreeSet<&str>`)。
let mut books = BTreeSet::new();
// 添加一些书。
books.insert("A Dance With Dragons");
books.insert("To Kill a Mockingbird");
books.insert("The Odyssey");
books.insert("The Great Gatsby");
// 检查一个特定的。
if !books.contains("The Winds of Winter") {
println!("We have {} books, but The Winds of Winter ain't one.",
books.len());
}
// 删除一本书。
books.remove("The Odyssey");
// 遍历所有内容。
for book in &books {
println!("{book}");
}
Run可以从数组初始化具有已知项列表的 BTreeSet
:
use std::collections::BTreeSet;
let set = BTreeSet::from([1, 2, 3]);
RunImplementations§
source§impl<T, A> BTreeSet<T, A>where
A: Allocator + Clone,
impl<T, A> BTreeSet<T, A>where A: Allocator + Clone,
sourcepub fn new_in(alloc: A) -> BTreeSet<T, A>
🔬This is a nightly-only experimental API. (btreemap_alloc
#32838)
pub fn new_in(alloc: A) -> BTreeSet<T, A>
btreemap_alloc
#32838)1.17.0 · sourcepub fn range<K, R>(&self, range: R) -> Range<'_, T> ⓘwhere
K: Ord + ?Sized,
T: Borrow<K> + Ord,
R: RangeBounds<K>,
pub fn range<K, R>(&self, range: R) -> Range<'_, T> ⓘwhere K: Ord + ?Sized, T: Borrow<K> + Ord, R: RangeBounds<K>,
在集合中元素的子范围上创建一个双端迭代器。
最简单的方法是使用范围语法 min..max
,因此 range(min..max)
将产生从最小 (inclusive) 到最大 (exclusive) 的元素。
也可以将范围输入为 (Bound<T>, Bound<T>)
,例如 range((Excluded(4), Included(10)))
将产生一个左排他的,范围从 4 到 10。
Panics
如果范围 start > end
,就会出现 panics。
如果范围 start == end
和两个边界均为 Excluded
,就会出现 panics。
Examples
use std::collections::BTreeSet;
use std::ops::Bound::Included;
let mut set = BTreeSet::new();
set.insert(3);
set.insert(5);
set.insert(8);
for &elem in set.range((Included(&4), Included(&8))) {
println!("{elem}");
}
assert_eq!(Some(&5), set.range(4..).next());
Runsourcepub fn difference<'a>(
&'a self,
other: &'a BTreeSet<T, A>
) -> Difference<'a, T, A> ⓘwhere
T: Ord,
pub fn difference<'a>( &'a self, other: &'a BTreeSet<T, A> ) -> Difference<'a, T, A> ⓘwhere T: Ord,
sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a BTreeSet<T, A>
) -> SymmetricDifference<'a, T> ⓘwhere
T: Ord,
pub fn symmetric_difference<'a>( &'a self, other: &'a BTreeSet<T, A> ) -> SymmetricDifference<'a, T> ⓘwhere T: Ord,
sourcepub fn intersection<'a>(
&'a self,
other: &'a BTreeSet<T, A>
) -> Intersection<'a, T, A> ⓘwhere
T: Ord,
pub fn intersection<'a>( &'a self, other: &'a BTreeSet<T, A> ) -> Intersection<'a, T, A> ⓘwhere T: Ord,
1.9.0 · sourcepub fn get<Q>(&self, value: &Q) -> Option<&T>where
T: Borrow<Q> + Ord,
Q: Ord + ?Sized,
pub fn get<Q>(&self, value: &Q) -> Option<&T>where T: Borrow<Q> + Ord, Q: Ord + ?Sized,
sourcepub fn is_disjoint(&self, other: &BTreeSet<T, A>) -> boolwhere
T: Ord,
pub fn is_disjoint(&self, other: &BTreeSet<T, A>) -> boolwhere T: Ord,
sourcepub fn is_subset(&self, other: &BTreeSet<T, A>) -> boolwhere
T: Ord,
pub fn is_subset(&self, other: &BTreeSet<T, A>) -> boolwhere T: Ord,
如果集合是另一个集合的子集,则返回 true
,即 other
至少包含 self
中的所有元素。
Examples
use std::collections::BTreeSet;
let sup = BTreeSet::from([1, 2, 3]);
let mut set = BTreeSet::new();
assert_eq!(set.is_subset(&sup), true);
set.insert(2);
assert_eq!(set.is_subset(&sup), true);
set.insert(4);
assert_eq!(set.is_subset(&sup), false);
Runsourcepub fn is_superset(&self, other: &BTreeSet<T, A>) -> boolwhere
T: Ord,
pub fn is_superset(&self, other: &BTreeSet<T, A>) -> boolwhere T: Ord,
如果该集合是另一个集合的超集,则返回 true
,即 self
至少包含 other
中的所有元素。
Examples
use std::collections::BTreeSet;
let sub = BTreeSet::from([1, 2]);
let mut set = BTreeSet::new();
assert_eq!(set.is_superset(&sub), false);
set.insert(0);
set.insert(1);
assert_eq!(set.is_superset(&sub), false);
set.insert(2);
assert_eq!(set.is_superset(&sub), true);
Run1.9.0 · sourcepub fn take<Q>(&mut self, value: &Q) -> Option<T>where
T: Borrow<Q> + Ord,
Q: Ord + ?Sized,
pub fn take<Q>(&mut self, value: &Q) -> Option<T>where T: Borrow<Q> + Ord, Q: Ord + ?Sized,
1.11.0 · sourcepub fn append(&mut self, other: &mut BTreeSet<T, A>)where
T: Ord,
A: Clone,
pub fn append(&mut self, other: &mut BTreeSet<T, A>)where T: Ord, A: Clone,
将所有元素从 other
移动到 self
,使 other
为空。
Examples
use std::collections::BTreeSet;
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);
assert_eq!(a.len(), 5);
assert_eq!(b.len(), 0);
assert!(a.contains(&1));
assert!(a.contains(&2));
assert!(a.contains(&3));
assert!(a.contains(&4));
assert!(a.contains(&5));
Run1.11.0 · sourcepub fn split_off<Q>(&mut self, value: &Q) -> BTreeSet<T, A>where
Q: Ord + ?Sized,
T: Borrow<Q> + Ord,
A: Clone,
pub fn split_off<Q>(&mut self, value: &Q) -> BTreeSet<T, A>where Q: Ord + ?Sized, T: Borrow<Q> + Ord, A: Clone,
按值将集合拆分为两个。 返回一个新集合,其中所有元素都大于或等于该值。
Examples
基本用法:
use std::collections::BTreeSet;
let mut a = BTreeSet::new();
a.insert(1);
a.insert(2);
a.insert(3);
a.insert(17);
a.insert(41);
let b = a.split_off(&3);
assert_eq!(a.len(), 2);
assert_eq!(b.len(), 3);
assert!(a.contains(&1));
assert!(a.contains(&2));
assert!(b.contains(&3));
assert!(b.contains(&17));
assert!(b.contains(&41));
Runsourcepub fn drain_filter<'a, F>(&'a mut self, pred: F) -> DrainFilter<'a, T, F, A> ⓘwhere
T: Ord,
F: 'a + FnMut(&T) -> bool,
🔬This is a nightly-only experimental API. (btree_drain_filter
#70530)
pub fn drain_filter<'a, F>(&'a mut self, pred: F) -> DrainFilter<'a, T, F, A> ⓘwhere T: Ord, F: 'a + FnMut(&T) -> bool,
btree_drain_filter
#70530)创建一个按升序访问所有元素的迭代器,并使用闭包来确定是否应删除元素。
如果闭包返回 true
,则该元素将从集合中移除并产生。如果闭包返回 false
或 panics,则该元素保留在集合中,并且不会被产生。
如果迭代器只被部分消耗或者根本没有被消耗,剩余的每个元素仍然会被闭包处理,如果返回 true
, 就会被移除和丢弃。
如果在闭包中出现 panic,或者如果在丢弃一个元素时出现 panic,或者如果 DrainFilter
本身被泄露,则未指定多少个元素会受到闭包的影响。
Examples
将一个集合分为偶数和奇数值,重新使用原始集合:
#![feature(btree_drain_filter)]
use std::collections::BTreeSet;
let mut set: BTreeSet<i32> = (0..8).collect();
let evens: BTreeSet<_> = set.drain_filter(|v| v % 2 == 0).collect();
let odds = set;
assert_eq!(evens.into_iter().collect::<Vec<_>>(), vec![0, 2, 4, 6]);
assert_eq!(odds.into_iter().collect::<Vec<_>>(), vec![1, 3, 5, 7]);
Runsourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
获取按升序访问 BTreeSet
中元素的迭代器。
Examples
use std::collections::BTreeSet;
let set = BTreeSet::from([1, 2, 3]);
let mut set_iter = set.iter();
assert_eq!(set_iter.next(), Some(&1));
assert_eq!(set_iter.next(), Some(&2));
assert_eq!(set_iter.next(), Some(&3));
assert_eq!(set_iter.next(), None);
Run迭代器返回的值以升序返回:
use std::collections::BTreeSet;
let set = BTreeSet::from([3, 1, 2]);
let mut set_iter = set.iter();
assert_eq!(set_iter.next(), Some(&1));
assert_eq!(set_iter.next(), Some(&2));
assert_eq!(set_iter.next(), Some(&3));
assert_eq!(set_iter.next(), None);
RunTrait Implementations§
source§impl<T, A> BitAnd<&BTreeSet<T, A>> for &BTreeSet<T, A>where
T: Ord + Clone,
A: Allocator + Clone,
impl<T, A> BitAnd<&BTreeSet<T, A>> for &BTreeSet<T, A>where T: Ord + Clone, A: Allocator + Clone,
source§impl<T, A> BitOr<&BTreeSet<T, A>> for &BTreeSet<T, A>where
T: Ord + Clone,
A: Allocator + Clone,
impl<T, A> BitOr<&BTreeSet<T, A>> for &BTreeSet<T, A>where T: Ord + Clone, A: Allocator + Clone,
source§impl<T, A> BitXor<&BTreeSet<T, A>> for &BTreeSet<T, A>where
T: Ord + Clone,
A: Allocator + Clone,
impl<T, A> BitXor<&BTreeSet<T, A>> for &BTreeSet<T, A>where T: Ord + Clone, A: Allocator + Clone,
1.2.0 · source§impl<'a, T, A> Extend<&'a T> for BTreeSet<T, A>where
T: 'a + Ord + Copy,
A: Allocator + Clone,
impl<'a, T, A> Extend<&'a T> for BTreeSet<T, A>where T: 'a + Ord + Copy, A: Allocator + Clone,
source§fn extend_one(&mut self, _: &'a T)
fn extend_one(&mut self, _: &'a T)
extend_one
#72631)source§impl<T, A> Extend<T> for BTreeSet<T, A>where
T: Ord,
A: Allocator + Clone,
impl<T, A> Extend<T> for BTreeSet<T, A>where T: Ord, A: Allocator + Clone,
source§fn extend<Iter>(&mut self, iter: Iter)where
Iter: IntoIterator<Item = T>,
fn extend<Iter>(&mut self, iter: Iter)where Iter: IntoIterator<Item = T>,
source§fn extend_one(&mut self, elem: T)
fn extend_one(&mut self, elem: T)
extend_one
#72631)