SortedList<>、SortedDictionary<>和字典<> [英] SortedList<>, SortedDictionary<> and Dictionary<>
问题描述
我发现 SortedList
SortedDictionary
和 Dictionary
实现了相同的接口.
- 我们什么时候应该选择
SortedList
和SortedDictionary
而不是Dictionary
? SortedList
和SortedDictionary
在应用方面有什么区别?
当对两者中的任何一个中的元素进行迭代时,都会对元素进行排序.
Dictionary
并非如此.MSDN 解决了
SortedList< 之间的区别;T,V>
和SortedDictionary
:
<块引用>
SortedDictionary(TKey, TValue) 泛型类是一个 二元搜索具有 O(log n) 检索的树,其中 n 是词典.在这方面,它类似于 SortedList(TKey,TValue) 泛型类.这两个类具有相似的对象模型,并且两者都有 O(log n) 检索.这两个类的不同之处在于内存使用和插入和删除速度:
SortedList(TKey, TValue) 比 SortedDictionary(TKey,T 值).
SortedDictionary(TKey, TValue) 插入和删除更快未排序数据的操作:O(log n) 而不是 O(n)SortedList(TKey, TValue).
如果列表是从排序的数据中一次性填充的,SortedList(TKey, TValue) 比 SortedDictionary(TKey,T 值).
I find that SortedList<TKey, TValue>
SortedDictionary<TKey, TValue>
and Dictionary<TKey, TValue>
implement the same interfaces.
- When should we opt for
SortedList
andSortedDictionary
overDictionary
? - What is the difference between
SortedList
andSortedDictionary
in terms of application?
When iterating over the elements in either of the two, the elements will be sorted. Not so with
Dictionary<T,V>
.MSDN addresses the difference between
SortedList<T,V>
andSortedDictionary<T,V>
:
The SortedDictionary(TKey, TValue) generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this respect, it is similar to the SortedList(TKey, TValue) generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:
SortedList(TKey, TValue) uses less memory than SortedDictionary(TKey, TValue).
SortedDictionary(TKey, TValue) has faster insertion and removal operations for unsorted data: O(log n) as opposed to O(n) for SortedList(TKey, TValue).
If the list is populated all at once from sorted data, SortedList(TKey, TValue) is faster than SortedDictionary(TKey, TValue).
这篇关于SortedList<>、SortedDictionary<>和字典<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!