简体   繁体   English

为此使用的最佳数据结构是什么?

[英]What is the best data structure to use for this?

I have to store a list of 3 values A,B and C. A and B are a pair of values. 我必须存储3个值A,B和C的列表。A和B是一对值。 C contains two values that describe what the record is, let's just say 1 and 2. C包含两个描述记录是什么的值,我们只说1和2。

For my purpose, I will be constantly looking up A and/or B to see whether a value exists or the pair exists. 出于我的目的,我将不断查找A和/或B以查看值是否存在或该对存在。

C is only used at the end of the processing to split it into two lists. C仅在处理结束时使用,以将其分为两个列表。

Further explaining what the data means -> A and B are just IDs and C is an action like Merge and Demerge. 进一步说明数据的含义-> A和B只是ID,而C是类似Merge和Demerge的操作。 So it looks like this: 所以看起来像这样:

1, 2, Merge 1,2,合并

3, 4, Demerge 3、4,德梅格

I have a bunch of rules that require me to look up whether the next A or B exist in 'any' IDs, or whether A and B are a pair in a record 我有一堆规则,要求我查询下一个A或B是否存在于“任何” ID中,或者A和B在记录中是否为对

Would it be better to use a List<Tuple<A,B,C>> or Dictionary<Tuple<A,B>,C> 最好使用List<Tuple<A,B,C>> or Dictionary<Tuple<A,B>,C>

Thanks 谢谢

In this case, a Dictionary seems to suit you better. 在这种情况下, Dictionary似乎更适合您。 As you said, you do not care about the C value until afterwards, nor is it part of your unique key. 如您所说,直到之后您才关心C值,它也不是唯一键的一部分。 A dictionary will force you to only have a single entry for any given Tuple<A,B> which it seems like you prefer (because you're only checking if a value or tuple already exists). 字典将强制您对于您似乎喜欢的任何给定的Tuple<A,B>仅具有一个条目(因为您仅检查值或元组是否已存在)。 A List would allow you to have duplicate entries. List将允许您有重复的条目。

Depending on your requirements, perhaps you can create a class to represent this object better (to avoid the additional overhead from Dictionary , as you seem to be using a minor subset of its features). 根据您的要求,也许您可​​以创建一个类来更好地表示该对象(以避免使用Dictionary的次要子集来避免Dictionary的额外开销)。 For example, do you need to iterate over each Tuple ? 例如,您是否需要遍历每个Tuple

我最终使用了DataTable,它可以完美地访问我需要的任意数量的列,以及进行更新,删除等操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM