The next key in the C # dictionary

How to get Enumerator for an element in -Sorted-dictionary using a key?

Note: GetEnumerator() gets the Enumerator for the first item.

But I need to get an Enumerator for an element with a given key in order to access the following elements using MoveNext() , for example ...

Edit: Or a way to access the following items ...

Edit: I prefer the const time method ...

thanks

+7
dictionary c # enumerator
source share
6 answers
 var enumerator = dictionary.Keys.SkipWhile(k => k != myKey) 

Where myKey is the key you are looking for. And you can use the OrderBy extension method if you want to sort the keys.

Change You cannot do this in constant using the / SortedDictionary dictionary. Why not implement your own binary search tree (e.g. SortedDictionary), and you will have O (log n) time search and O (1) .next() ?

+7
source share

You cannot do this with Dictionary . You can do this with index access, so you can use a SortedList instead of a dictionary. You can also watch SkipWhile .

Although you might have a workaround like this:

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

But of course, this is not a good idea.

+1
source share

If you have Framework> = 3.5 installed, use SkipWhile Janus Tuning and Lyukh suggested. For versions with a lower version, you must do this for yourself (for example, fill out the second dictionary using keyvaluepairs from your key to the end).

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

The easiest option is to use a SortedList , and then add an extension method to it that returns an IEnumerable whose elements are greater than or equal to the given key. The complexity of the GetElementsGreaterThanOrEqual method below is O (log (n)) to get the first element, and then each iteration after that is 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
source share

Perhaps this is useful to someone:

 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 
0
source share

All Articles