Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next key in C# Dictionary

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

Note:GetEnumerator() gets an Enumerator to first element..

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

Edit: Or a way to access next elements...

Edit: I prefer a const time method...

Thanks

like image 979
Betamoo Avatar asked Jul 27 '10 09:07

Betamoo


People also ask

What is the use of next function in C++?

std::next in C++. std::next returns an iterator pointing to the element after being advanced by certain no. of positions. It is defined inside the header file . It does not modify its arguments and returns a copy of the argument advanced by the specified amount. If it is a random-access iterator, the function uses just once operator + or ...

What is the use of return keyword in C++?

The short, long, signed and unsigned keywords are type modifiers that alter the meaning of a base data type to yield a new type. The return keyword terminates the function and returns the value. This function func () returns 5 to the calling function.

What are the key words in C programming?

Keywords in C Programming; auto: break: case: char: const: continue: default: do: double: else: enum: extern: float: for: goto: if: int: long: register: return: short: signed: sizeof: static: struct: switch: typedef: union: unsigned: void: volatile: while

What is the use of extern keyword in C?

The extern keyword declares that a variable or a function has external linkage outside of the file it is declared. To learn more, visit C storage type. There are three types of loops in C programming. The for loop is written in C programming using the keyword for.


1 Answers

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

Where myKey is the key you're looking for. And you can use the OrderBy extension method if you want to have the keys sorted.

Edit: You can't do it in constant with Dictionary/SortedDictionary. Why not implement your own binary search tree (like SortedDictionary is) and you will have O(log n) time lookup and O(1) time .next()?

like image 172
Janus Tøndering Avatar answered Sep 28 '22 11:09

Janus Tøndering