为什么没有SortedList< T>在.NET中? [英] Why is there no SortedList<T> in .NET?
问题描述
为什么只有 SortedList< TKey,TValue>
看起来更像是字典,却没有 SortedList< T>
实际上只是始终排序的列表吗?
Why is there only a SortedList<TKey, TValue>
which looks more like a dictionary, but no SortedList<T>
that is actually just a list that is always sorted?
根据有关SortedList的MSDN文档,它实际上在内部实现为 KeyValuePair< TKey,TValue>
始终按键排序。同一类作为 T
类型的列表是否有用?
According to the MSDN documentation on SortedList, it is actually internally implemented as a dynamically-sized array of KeyValuePair<TKey, TValue>
that is always sorted by the key. Wouldn’t the same class be more useful as a list of any type T
? Wouldn’t that fit the name better, too?
推荐答案
尽管没有人能真正告诉您为什么没有 SortedList< T>
,可以讨论为什么 SortedList
需要一个键和一个值。字典将键映射到值。典型的方法是使用二叉树,哈希表和列表(数组),尽管哈希表最常见,因为它们在大多数操作中都是O(1)。
Although nobody can really tell you why there is no SortedList<T>
, it is possible to discuss why SortedList
takes a key and a value. A dictionary maps keys to values. The typical ways to do this are with a binary tree, a hash table, and a list (array), though hash tables are most common because they are O(1) for most operations.
O(1)中不支持的主要操作是按顺序获取下一个键。如果您希望这样做,通常可以使用二叉树,为您提供排序字典。
The primary operation that it doesn't support in O(1) is getting the next key in order. If you want to be able to do that, you typically use a binary tree, giving you a sorted dictionary.
如果您决定将地图实现为列表,则可以将使元素按键排序,以便查找为O(lg n),从而为您提供另一个排序字典-以排序列表的形式。当然已经使用了名称 SortedDictionary
,但没有使用 SortedList
。我可能将其称为 SortedListDictionary
或 SortedDictionaryList
,但我没有给它命名。
If you decide to implement the map as a list, you would keep the elements sorted by key so that lookup is O(lg n), giving you another sorted dictionary -- in the form of a sorted list. Of course the name SortedDictionary
was already taken, but SortedList
wasn't. I might have called it SortedListDictionary
or SortedDictionaryList
, but I didn't get to name it.
这篇关于为什么没有SortedList< T>在.NET中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!