【问题标题】:C#: Is a SortedDictionary sorted when you enumerate over it?C#:SortedDictionary 枚举时是否已排序?
【发布时间】:2010-11-11 07:02:06
【问题描述】:

SorteDictionary 是根据 MSDN 对键进行排序的。这是否意味着您可以确定在 foreach 中枚举它时会对其进行排序?还是仅仅意味着 SortedDictionary 在内部以这种方式工作以在各种情况下具有更好的性能?

【问题讨论】:

    标签: c# sorting dictionary enumeration


    【解决方案1】:

    From MSDN:

    字典保存在一个 使用内部树排序。 每个新元素都位于 正确的排序位置,树是 调整以保持排序顺序 每当一个元素被删除。尽管 枚举,排序顺序是 维护。

    【讨论】:

    • 大声笑。它在哪里说呢?我读起来像......所有这一切......一定是瞎了......
    【解决方案2】:

    当您枚举集合时,它按键排序(即使您枚举说Values 集合)。在内部,集合被实现为二叉搜索树(根据文档)。值的插入和查找都是 O(log n)(这意味着它们非常有效)。

    【讨论】:

      【解决方案3】:

      是的,就是这个意思。

      编辑:“这是否意味着您可以确定在 foreach 中枚举它时它会被排序?”的部分?

      【讨论】:

      • 是否保证已排序? (与没有的常规字典相比)
      【解决方案4】:

      如果您枚举SortedDictionary 中的项目,这些项目将按照项目键的排序顺序返回。如果您通过SortedDictionary 中的键进行枚举,则键也将按排序顺序返回。也许有些令人惊讶的是,如果您按其值枚举 SortedDictionary,则返回的值按键的排序顺序,而不是您可能期望的值的排序顺序。

      演示:

      请注意,在此演示中,添加到 SortedDictionary 的项目按排序顺序添加。

      另外,如果您打算按字典的值枚举字典,并且可能出现重复值,请考虑使用反向查找函数return an IEnumerable<T>。 (当然,对于大型字典,按值查找键可能会导致性能不佳。)

      using System;
      using System.Collections.Generic;
      using System.Linq;
      
      class SortedDictionaryEnumerationDemo
      {
          static void Main()
          {
              var dict = new SortedDictionary<int, string>();
              dict.Add(4, "Four");
              dict.Add(5, "Five");
              dict.Add(1, "One");
              dict.Add(3, "Three");
              dict.Add(2, "Two");
      
              Console.WriteLine("== Enumerating Items ==");
              foreach (var item in dict)
              {
                  Console.WriteLine("{0} => {1}", item.Key, item.Value);
              }
      
              Console.WriteLine("\n== Enumerating Keys ==");
              foreach (int key in dict.Keys)
              {
                  Console.WriteLine("{0} => {1}", key, dict[key]);
              }
      
              Console.WriteLine("\n== Enumerating Values ==");
              foreach (string value in dict.Values)
              {
                  Console.WriteLine("{0} => {1}", value, GetKeyFromValue(dict, value));
              }
          }
      
          static int GetKeyFromValue(SortedDictionary<int, string> dict, string value)
          {
              // Use LINQ to do a reverse dictionary lookup.
              try
              {
                  return
                      (from item in dict
                       where item.Value.Equals(value)
                       select item.Key).First();
              }
              catch (InvalidOperationException e)
              {
                  return -1;
              }
          }
      }
      

      预期输出:

      == Enumerating Items ==
      1 => One
      2 => Two
      3 => Three
      4 => Four
      5 => Five
      
      == Enumerating Keys ==
      1 => One
      2 => Two
      3 => Three
      4 => Four
      5 => Five
      
      == Enumerating Values ==
      One => 1
      Two => 2
      Three => 3
      Four => 4
      Five => 5
      

      【讨论】:

        猜你喜欢
        • 2011-08-11
        • 2017-07-21
        • 2013-05-09
        • 1970-01-01
        • 2012-01-25
        • 1970-01-01
        • 2011-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多