标签: sorteddictionary

SortedList和SortedDictionary有什么区别?

a SortedList<TKey,TValue>和a 之间是否有任何实际的区别SortedDictionary<TKey,TValue>?在任何情况下你会专门使用一个而不是另一个吗?

.net c# generics sortedlist sorteddictionary

251
推荐指数
6
解决办法
9万
查看次数

SortedList <>,SortedDictionary <>和Dictionary <>

我发现SortedList<TKey, TValue> SortedDictionary<TKey, TValue>Dictionary<TKey, TValue>实现相同的接口.

  1. 什么时候应该选择SortedList还是SortedDictionary结束Dictionary
  2. 之间有什么区别SortedListSortedDictionary应用方面?

c# generics dictionary sortedlist sorteddictionary

85
推荐指数
5
解决办法
4万
查看次数

何时使用SortedList <TKey,TValue>而不是SortedDictionary <TKey,TValue>?

这似乎与这个问题重复,后者询问" SortedListSortedDictionary之间有什么区别?" 不幸的是,答案只是引用MSDN文档(它清楚地表明两者之间存在性能和内存使用差异),但实际上并没有回答这个问题.

事实上(根据MSDN,这个问题没有得到相同的答案):

SortedList<TKey, TValue>通用类是O(log n)的检索,其中n是字典中的元件的数目的二进制搜索树.在这里,它类似于 SortedDictionary<TKey, TValue>泛型类.这两个类具有相似的对象模型,并且都具有O(log n)检索.两个类别的不同之处在于内存使用和插入和移除速度:

  • SortedList<TKey, TValue>使用的内存少于SortedDictionary<TKey, TValue>.

  • SortedDictionary<TKey, TValue>对于未排序的数据,O(log n)具有更快的插入和删除操作,而不是O(n) SortedList<TKey, TValue>.

  • 如果列表是从排序数据中一次性填充的,SortedList<TKey, TValue>则速度快于 SortedDictionary<TKey, TValue>.

所以,显然这表明这SortedList<TKey, TValue>是更好的选择,除非您需要更快的插入和删除未排序数据的操作.

问题仍然存在,鉴于以上信息,使用a的实际(现实世界,商业案例等)原因是SortedDictionary<TKey, TValue>什么?根据绩效信息,这意味着根本没有必要SortedDictionary<TKey, TValue>.

.net c# sortedlist sorteddictionary

63
推荐指数
5
解决办法
4万
查看次数

SortedList与SortedDictionary vs. Sort()

这是像这样的问题的延续.

是否有任何调整性能的指导原则?我并不是指大O的收益,只是节省一些线性时间.

例如,要花多少钱预分类保存在任SortedListSortedDictionary

假设我有一个有3个属性的人类排序,其中一个是年龄.我应该先按年龄换取物品吗?

我应该首先对一个属性进行排序,然后使用结果列表/字典对两个属性进行排序,依此类推?

想到的任何其他优化?

.net sorting performance sortedlist sorteddictionary

22
推荐指数
1
解决办法
2万
查看次数

Python 2.6 TreeMap/SortedDictionary?

在Python 2.6中是否有内置的排序字典实现,或者哈希表是唯一的类型?

澄清:

  • 我问的是有关排序的词典,而不是 有序的词典!

python dictionary python-2.6 treemap sorteddictionary

19
推荐指数
2
解决办法
2万
查看次数

什么时候应该使用sorteddictionary而不是字典

正如我在上一篇文章中写的那样,我对c#世界还是一个新手,所以我写了一个小基准来比较Dictionary,Hashtable,SortedList和SortedDictionary.该测试运行8000次迭代,从50到100000个元素.我测试了添加新元素,搜索元素并循环遍历一些元素.结果就像我预期的那样,除了SortedDictionary的结果,这让我感到很困惑......所有结果都很慢.所以我错过了关于排序字典概念的一些内容.我已经问了谷歌,但我发现的所有其他人都得到了相同的测试结果.根据他们的测试实施略有不同.我的问题再次出现:为什么SortedDicrionary比其他所有人慢得多?

c# benchmarking dictionary sorteddictionary

18
推荐指数
2
解决办法
1万
查看次数

如何从SortedDictionary获取以前的密钥?

我有包含键值对的字典.

SortedDictionary<int,int> dictionary=new SortedDictionary<int,int>();
dictionary.Add(1,33);
dictionary.Add(2,20);
dictionary.Add(4,35);
Run Code Online (Sandbox Code Playgroud)

我想从已知的键值获取先前的键值对.在上面的例子中,如果我有键4,那我怎么能得到<2,20>

.net c# linq sorteddictionary

16
推荐指数
2
解决办法
7021
查看次数

SortedDictionary是红黑树吗?

我在互联网上看到了几个关于此的引用,但没有官方文档?谁能告诉我在哪里可以获得有关此信息?

.net c# binary-tree sorteddictionary

15
推荐指数
3
解决办法
1万
查看次数

如何使用CustomedDictionary的自定义IComparer?

我很难将自定义IComparer用于我的SortedDictionary <>.目标是将电子邮件地址以特定格式(firstnam.lastname@domain.com)作为密钥,并按姓氏排序.当我做这样的事情时:

public class Program
{
  public static void Main(string[] args)
  {
    SortedDictionary<string, string> list = new SortedDictionary<string, string>(new SortEmailComparer());
    list.Add("a.johansson@domain.com", "value1");
    list.Add("b.johansson@domain.com", "value2");
    foreach (KeyValuePair<string, string> kvp in list)
    {
      Console.WriteLine(kvp.Key);
    }
    Console.ReadLine();
  }
}

public class SortEmailComparer : IComparer<string>
{
  public int Compare(string x, string y)
  {
    Regex regex = new Regex("\\b\\w*@\\b",
                        RegexOptions.IgnoreCase
                        | RegexOptions.CultureInvariant
                        | RegexOptions.IgnorePatternWhitespace
                        | RegexOptions.Compiled
                        );

    string xLastname = regex.Match(x).ToString().Trim('@');
    string yLastname = regex.Match(y).ToString().Trim('@');
    return xLastname.CompareTo(yLastname);
  }
}
Run Code Online (Sandbox Code Playgroud)

我得到这个ArgumentException: An entry with the same …

c# icomparer sorteddictionary

11
推荐指数
1
解决办法
2万
查看次数

为什么SortedDictionary <K,V> .GetEnumerator O(log n)但SortedSet <T> .GetEnumerator O(1)?

SortedSet<T>.GetEnumerator文档:

该方法是O(1)操作

SortedDictionary<K, V>.GetEnumerator文档:

此方法是O(log n)操作,其中n是count.

这两个陈述都可以是真的,考虑到SortedDictionary<K, V>内部实现为SortedSet<KeyValuePair<K, V>?我检查了类的GetEnumerator代码SortedDictionary- 它直接使用了SortedSet枚举器.我注意到了SortedSet枚举器的实现,在我看来它确实有O(log n)特性(这里是代码):

public SortedSet<T>.Enumerator GetEnumerator()
{
  return new SortedSet<T>.Enumerator(this);
}

//which calls this constructor:
internal Enumerator(SortedSet<T> set)
{
  this.tree = set;
  this.tree.VersionCheck();
  this.version = this.tree.version;
  this.stack = new Stack<SortedSet<T>.Node>(2 * SortedSet<T>.log2(set.Count + 1));
  this.current = (SortedSet<T>.Node) null;
  this.reverse = false;
  this.siInfo = (SerializationInfo) null;
  this.Intialize();
}

private void Intialize()
{
  this.current = (SortedSet<T>.Node) null;
  SortedSet<T>.Node …
Run Code Online (Sandbox Code Playgroud)

.net big-o sortedset sorteddictionary asymptotic-complexity

11
推荐指数
1
解决办法
286
查看次数