SortedList<>、SortedDictionary<>和字典&lt;&gt; [英] SortedList&lt;&gt;, SortedDictionary&lt;&gt; and Dictionary&lt;&gt;

查看:22
本文介绍了SortedList<>、SortedDictionary<>和字典&lt;&gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 SortedList SortedDictionaryDictionary 实现了相同的接口.

  1. 我们什么时候应该选择 SortedListSortedDictionary 而不是 Dictionary?
  2. SortedListSortedDictionary 在应用方面有什么区别?

解决方案

  1. 当对两者中的任何一个中的元素进行迭代时,都会对元素进行排序.Dictionary 并非如此.

  2. 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.

  1. When should we opt for SortedList and SortedDictionary over Dictionary?
  2. What is the difference between SortedList and SortedDictionary in terms of application?

解决方案

  1. When iterating over the elements in either of the two, the elements will be sorted. Not so with Dictionary<T,V>.

  2. MSDN addresses the difference between SortedList<T,V> and SortedDictionary<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<>和字典&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆