什么是排序列表和SortedDictionary之间的区别?区别、列表、SortedDictionary

2023-09-02 10:17:22 作者:坚辛岁月

有没有之间的SortedList<TKey,TValue>和SortedDictionary<TKey,TValue>?是否有任何情况下,你会专门使用一个而不是其他?

Is there any real practical difference between a SortedList<TKey,TValue> and a SortedDictionary<TKey,TValue>? Are there any circumstances where you would specifically use one and not the other?

推荐答案

是 - 它们的性能特点显著不同。或许,这将是更好地给他们打电话排序列表 SortedTree 因为这反映了实现更加紧密。

Yes - their performance characteristics differ significantly. It would probably be better to call them SortedList and SortedTree as that reflects the implementation more closely.

看MSDN文档为他们每个人( 排序列表 SortedDictionary ),用于在不同situtations不同的操作的性能的详细信息。这里是一个很好的总结(从 SortedDictionary 文档):

Look at the MSDN docs for each of them (SortedList, SortedDictionary) for details of the performance for different operations in different situtations. Here's a nice summary (from the SortedDictionary docs):

SortedDictionary&LT; TKEY的,TValue&GT; 通用   类是用二进制搜索树   O(log n)的检索,其中n是   在字典中的元素数目。   在此,它是类似于   排序列表&LT; TKEY的,TValue&GT; 通用   类。这两个类具有相似的   对象模型,并且都为O(log n)的   检索。其中,这两个类   不同之处在于内存使用和速度   插入和删除:

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, 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:   

排序列表&LT; TKEY的,TValue&GT; 使用较少   内存比 SortedDictionary&LT; TKEY的,   TValue&GT;

SortedList<TKey, TValue> uses less memory than SortedDictionary<TKey, TValue>.

SortedDictionary&LT; TKEY的,TValue&GT; 有   更快的插入和移除   对于未排序的数据操作,O(log n)的   而不是为O(n),用于   排序列表&LT; TKEY的,TValue&GT;

SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<TKey, TValue>.

如果该列表中填充一次全部   从排序的数据,排序列表&LT; TKEY的,   TValue&GT; 比快    SortedDictionary&LT; TKEY的,TValue&GT;

If the list is populated all at once from sorted data, SortedList<TKey, TValue> is faster than SortedDictionary<TKey, TValue>.

排序列表其实维护一个有序数组,而不是使用一棵树,它仍然使用二进制搜索找到的元素。)

(SortedList actually maintains a sorted array, rather than using a tree. It still uses binary search to find elements.)