stl 集的 C# 等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/575406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the C# equivalent of the stl set?
提问by MrDatabase
I want to store some values in a balanced binary search tree using C#. I looked through the collections in the generics namespace and I haven't found an equivalent of the stl set.
我想使用 C# 在平衡的二叉搜索树中存储一些值。我查看了泛型命名空间中的集合,但没有找到与 stl 集等效的集合。
What generic collection can I use? (I don't want to store key/value pairs... just values.)
我可以使用什么通用集合?(我不想存储键/值对……只是值。)
采纳答案by Szymon Rozga
If you require sorted set, use
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. 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>
.Otherwise, check out C5 Generic Collection Library. In particular
TreeSet<T>
. It is a red-black tree and only stores the values.
如果您需要排序集,请使用
SortedDictionary<T,U>
. 这是使用二叉搜索树实现的。诚然,每个条目将使用 64 位,因为您在下面存储了一个键值对。您可以像这样围绕它编写一个包装器:class Set<T> : SortedDictionary<T, bool> { public void Add(T item) { this.Add(item, true); } }
如果不需要排序集,请使用
HashSet<T>
.否则,请查看C5 通用集合库。特别是
TreeSet<T>
。它是一棵红黑树,只存储值。
回答by Luca Martinetti
You could use an HashSet
您可以使用HashSet
The
HashSet<T>
class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
本
HashSet<T>
类提供高性能的一组操作。集合是一个不包含重复元素的集合,其元素没有特定的顺序。
The capacity of a HashSet<T>
object is the number of elements that the object can hold. A HashSet<T>
object's capacity automatically increases as elements are added to the object.
HashSet<T>
对象的容量是对象可以容纳的元素数。甲HashSet<T>
作为元素被添加到对象物体的能力自动增加。
回答by ChrisW
HashSet, but HashSet isn't available in version 2.0 of the framework. If you need something for 2.0 then use Dictionary, and specify a dummy type (e.g. object, int, or bool) for which you supply a dummy value (e.g. null, 0, or false) as the second/value parameter (i.e. use the Dictionary as a set of keys without caring about the associated values).
HashSet,但 HashSet 在框架的 2.0 版中不可用。如果您需要 2.0 的某些内容,则使用 Dictionary,并指定一个虚拟类型(例如 object、int 或 bool),您为其提供一个虚拟值(例如 null、0 或 false)作为第二个/值参数(即使用字典作为一组键而不关心关联的值)。
回答by chakrit
Try RedBlackTree.NET. It's in VB but I think it can be easily converted to C#.
试试RedBlackTree.NET。它在 VB 中,但我认为它可以轻松转换为 C#。
And I believe some of the collection type actually uses a red-black tree internally. So you might want to decompile the frameworkitself and look around for some clues.
而且我相信一些集合类型实际上在内部使用了红黑树。因此,您可能想要反编译框架本身并四处寻找一些线索。
I don't think a binary tree can be replaced by a HashSet. Their performance characteristics are different, roughly:
我不认为二叉树可以用 HashSet 代替。它们的性能特点各不相同,大致如下:
HashSet - O(1) lookup (n) search
Binary search tree - O(log n) lookup O(log n) search
HashSet - O(1) 查找 (n) 搜索 二
叉搜索树 - 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。