C#字典中的下一个键 [英] Next key in C# Dictionary

查看:323
本文介绍了C#字典中的下一个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用密钥获得一个枚举器到一个字典中的项目?



注意: GetEnumerator()获取一个枚举器到第一个元素..



但是,我需要使用给定的键获取一个枚举器到元素,以便使用 MoveNext()例如...



编辑:或一种访问下一个元素的方法...



编辑:我更喜欢一个const时间方法...



谢谢

解决方案

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

myKey是您要查找的关键字。而您可以使用OrderBy扩展方法,如果您希望按键排序。



编辑:您不能以常量方式词典/ SortedDictionary。为什么不实现自己的二叉搜索树(如SortedDictionary是),你将有O(log n)时间查找和O(1)时间 .next()? p>

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

解决方案

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()?

这篇关于C#字典中的下一个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆