当键和值同等重要的数据结构 [英] Data structure for when key and value are equally "important"
问题描述
所以,这可能是一个愚蠢的问题,但是我将唯一的ID映射到唯一的值。有时我想要一个ID的值,有时我想知道一个值的ID。我搜索超过我修改的集合。我想知道是否有一个特殊的数据结构在这里是有意义的,或者如果我应该保持两个副本的集合(从来不是超大的) - 一个由ID键入的值,另一个由值键入。
谢谢!
维护两个副本的集合是规范的解决方案。请注意,如果合适,两个方向可以使用不同的集合类型(例如,具有不同散列函数的散列表,散列表和平衡树等)。
So, this is probably a stupid question, but I have a mapping of unique IDs to unique values. Sometimes I want the value for a certain ID, sometimes I want to know the ID of a certain value. I search more than I modify the collection. I'm wondering if there's a special datastructure that makes sense here, or if I should just maintain two copies of the collection (which is never super large)--one keyed by ID, and one keyed by value.
Thanks!
Maintaining two copies of the collection is the canonical solution.
Note that the two directions can use different collection types if appropriate (e.g. hash tables with different hash functions, a hash table and a balanced tree, etc.).
这篇关于当键和值同等重要的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!