2010-07-27 53 views
7

如何使用键将Enumerator转换为-Sorted-词典中的项目?C#词典中的下一个键

注:GetEnumerator()得到一个Enumerator到第一个元素..

但我需要得到一个Enumerator与给定键的元素,以获取使用MoveNext()例如下一个元素...

编辑:或者访问下一个元素的方式...

编辑:我更喜欢一个常量时间的方法...

感谢

回答

7
var enumerator = dictionary.Keys.SkipWhile(k => k != myKey) 

哪里的myKey是你要找的关键。如果你想把键排序,你可以使用OrderBy扩展方法。

编辑:你不能在Dictionary/SortedDictionary中使用常量。为什么不实现你自己的二叉搜索树(就像SortedDictionary一样),你将有O(log n)时间查询和O(1)时间.next()

1

你不能用Dictionary做到这一点。 您可以完成那些有索引访问的可能性,因此您可以使用SortedList而不是Dictionary。你也可以看看SkipWhile

虽然你可以有一些解决办法是这样的:

Dictionary<int, int> dictionary = new Dictionary<int, int>(); 
foreach (KeyValuePair<int, int> pair in dictionary) 
{ 
    // you can check the key you need and assume that the next one will be what you need. 
} 

但是,当然,这是不是最好的主意。

0
var query = yourDictionary.SkipWhile(kvp => kvp.Key != keyToFind); 
foreach (var result in query) 
{ 
    // ... 
} 
1

如果你有框架> = 3.5安装使用SkipWhile剑锋Tondering和LukeH建议。 对于较低的框架版本,您必须为自己完成这一工作(即使用从您的密钥到最后的keyvalue对填充第二个字典)。

0

最简单的选择是使用SortedList,然后向其添加一个扩展方法,它返回一个IEnumerable,其元素大于或等于给定的键。下面的GetElementsGreaterThanOrEqual方法的复杂性是O(log(n))以获得第一个元素,然后每个迭代之后是O(1)。

public static class SortedListExtension 
{ 
    public static IEnumerable<KeyValuePair<TKey, TValue>> GetElementsGreaterThanOrEqual<TKey, TValue>(this SortedList<TKey, TValue> instance, TKey target) where TKey : IComparable<TKey> 
    { 
     int index = instance.BinarySearch(target); 
     if (index < 0) 
     { 
      index = ~index; 
     } 
     for (int i = index; i < instance.Count; i++) 
     { 
      yield return new KeyValuePair<TKey, TValue>(instance.Keys[i], instance.Values[i]); 
     } 
    } 

    public static int BinarySearch<TKey, TValue>(this SortedList<TKey, TValue> instance, TKey target) where TKey : IComparable<TKey> 
    { 
     int lo = 0; 
     int hi = instance.Count - 1; 
     while (lo <= hi) 
     { 
      int index = lo + ((hi - lo) >> 1); 
      int compare = instance.Keys[index].CompareTo(target); 
      if (compare == 0) 
      { 
       return index; 
      } 
      else 
      { 
       if (compare < 0) 
       { 
        lo = index + 1; 
       } 
       else 
       { 
        hi = index - 1; 
       } 
      } 
     } 
     return ~lo; 
    } 
} 
+0

将如何使用此方法/叫什么? – vapcguy 2016-09-22 20:30:03

0

也许这是有用的人:

public Dictionary<string, int> myDictionary = new Dictionary<string, int>(); 
public string myCurrentKey = "some key 5"; 
for (int i = 1; i <= 10; i++) { 
    myDictionary.Add(string.Format("some key {0}", i), i); 
} 

private void MoveIndex(int dir) { // param "dir" can be 1 or -1 to move index forward or backward 
    List<string> keys = new List<string>(myDictionary.Keys); 
    int newIndex = keys.IndexOf(myCurrentKey) - dir; 
    if (newIndex < 0) { 
     newIndex = myDictionary.Count - 1; 
    } else if (newIndex > myDictionary.Count - 1) { 
     newIndex = 0; 
    } 

    myCurrentKey = keys[newIndex]; 
} 

Debug.Log(string.Format("Current value: {0}", myDictionary[myCurrentKey])); // prints 5 
MoveIndex(1); 
Debug.Log(string.Format("Current value: {0}", myDictionary[myCurrentKey])); // prints 6 
MoveIndex(-1); 
MoveIndex(-1); 
Debug.Log(string.Format("Current value: {0}", myDictionary[myCurrentKey])); // prints 4