[英]What is the C# equivalent of the stl set?
I want to store some values in a balanced binary search tree using C#.我想使用 C# 在平衡的二叉搜索树中存储一些值。 I looked through the collections in the generics namespace and I haven't found an equivalent of the stl set.我查看了泛型命名空间中的集合,但没有找到与 stl 集等效的集合。
What generic collection can I use?我可以使用什么通用集合? (I don't want to store key/value pairs... just values.) (我不想存储键/值对……只是值。)
You could use an HashSet您可以使用HashSet
The
HashSet<T>
class provides high performance set operations.HashSet<T>
类提供高性能的集合操作。 A set is a collection that contains no duplicate elements , and whose elements are in no particular order.集合是一个不包含重复元素的集合,其元素没有特定的顺序。
The capacity of a HashSet<T>
object is the number of elements that the object can hold. HashSet<T>
对象的容量是该对象可以容纳的元素数。 A HashSet<T>
object's capacity automatically increases as elements are added to the object. HashSet<T>
对象的容量会随着元素添加到对象中而自动增加。
If you require sorted set, use SortedDictionary<T,U>
.如果您需要排序集,请使用SortedDictionary<T,U>
。 This is implemented using a binary search tree.这是使用二叉搜索树实现的。 Admittedly, you will be using 64-bits per entry because you are storing a key-value pair underneath.不可否认,每个条目将使用 64 位,因为您在下面存储了一个键值对。 You can write a wrapper around it like this:您可以像这样围绕它编写一个包装器:
class Set<T> : SortedDictionary<T, bool> { public void Add(T item) { this.Add(item, true); } }
If you don't require a sorted set, use HashSet<T>
.如果不需要排序集,请使用HashSet<T>
。
Otherwise, check out C5 Generic Collection Library .否则,请查看C5 通用集合库。 In particular TreeSet<T>
.特别是TreeSet<T>
。 It is a red-black tree and only stores the values.它是一棵红黑树,只存储值。
Try RedBlackTree.NET .试试RedBlackTree.NET 。 It's in VB but I think it can be easily converted to C#.它在 VB 中,但我认为它可以轻松转换为 C#。
And I believe some of the collection type actually uses a red-black tree internally .而且我相信一些集合类型实际上在内部使用了红黑树。 So you might want to decompile the framework itself and look around for some clues.因此,您可能想要反编译框架本身并四处寻找一些线索。
I don't think a binary tree can be replaced by a HashSet.我不认为二叉树可以用 HashSet 代替。 Their performance characteristics are different, roughly:它们的性能特点各不相同,大致如下:
HashSet - O(1) lookup (n) search HashSet - O(1) 查找 (n) 搜索
Binary search tree - O(log n) lookup O(log n) search二叉搜索树 - O(log n) 查找 O(log n) 搜索
If you want to store the values and later perform a search, you will want to be using a binary tree instead of a HashSet.如果要存储值并稍后执行搜索,则需要使用二叉树而不是 HashSet。
HashSet, but HashSet isn't available in version 2.0 of the framework. HashSet,但 HashSet 在框架的 2.0 版中不可用。 If you need something for 2.0 then use Dictionary, and specify a dummy type (eg object, int, or bool) for which you supply a dummy value (eg null, 0, or false) as the second/value parameter (ie use the Dictionary as a set of keys without caring about the associated values).如果您需要 2.0 的某些内容,则使用 Dictionary,并指定一个虚拟类型(例如 object、int 或 bool),您为其提供一个虚拟值(例如 null、0 或 false)作为第二个/值参数(即使用字典作为一组键而不关心关联的值)。
我知道这个问题很老,但对于 .NET Framework 4.0 及更高版本,请使用SortedSet<T>
或ImmutableSortedSet<T>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.