如何顺序访问字典? [英] How to sequentialy access a dictionary?

查看:118
本文介绍了如何顺序访问字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以元素存储在其中的方式访问字典。有人可以帮我这么做吗?在此先感谢!!

I want to access the dictionary the way element was stored in it. Could anybody help me do this. Thanks in advance!!

推荐答案

字典将其值存储在由键索引的结构中,或者更确切地说是键的哈希值。这就是他们快速的原因。他们不需要搜索值,只需获取键的has值并直接获取值(在大多数情况下,只有在碰撞键值哈希值时才需要搜索)。

Dictionaries store its values in a structure indexed by keys or more precisely by a hash values of keys. This is why they are fast. They do not need to search for values, they just take the has values of a key and go straight for the value (in most cases, only in case of colliding key hash values they have to search).

因此保存值的顺序是不可预测的。如果您需要订单,则需要数组或链接列表 - 两种结构都有一个定义的顺序,其中存储了值。

So the order in which values are being saved is not predictable. If you need the order you need an Array or Linked List - both structures have a defined order in which values are being stored.

如果您需要两者:字典类型访问保存顺序你必须寻找一个带有列表支持字典的可能开源库或者你自己的版本:采用字典界面并实现它,以便它同时保存在列表和内部字典中。

If you need both: dictionary-type access and preservation of order you would have to look for a possibly open source library with a list backed dictionary or do you own version: take a dictionary interface and implement it so that it gets saved in a lists and internal dictionary at the same time.

访问器方法将转到字典,但您可以提供列表的迭代器并按添加顺序返回数据。

Accessor methods would go to the dictionary but you could provide iterators that go for the list and return data in the order of addition.

这篇关于如何顺序访问字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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