gpt4 book ai didi

arrays - 我可以使用集合类型作为数组索引吗?

转载 作者:行者123 更新时间:2023-12-03 14:46:43 24 4
gpt4 key购买 nike

我无法使用set类型作为数组的大小指示符,但是对于小集合这样做是完全明智的。

假设我有以下代码:

  TFutureCoreSet = set of 0..15;
TLookupTable = record
FData: array[TFutureCoreSet] of TSomeRecord; //error ordinal type required
....

以下代码可以编译并运行。

  TFutureCoreSet = set of 0..15;
TLookupTable = record
FData: array[word] of TSomeRecord;

但这会破坏 TFutureCoreSet 中允许的状态数与查找表中的元素之间的链接。
有没有一种简单的方法来链接两者,以便当一个更改时其他也更新?

最佳答案

只是稍微不同一下:

type
TFutureCore = 0..15;
TFutureCoreSet = set of TFutureCore;
TFutureCoreIndex = 0..(2 shl High(TFutureCore)) - 1;
TLookupTable = record
FData: array[TFutureCoreIndex] of TSomeRecord;
end;

使用 TFutureCoreIndex 的另一个优点是,您可以使用它将 TFutureCoreSet 类型转换为序数类型。当对集合类型进行类型转换时,您必须转换为相同大小的序数类型。

AllowedStates = LookupTable.FData[TFutureCoreIndex(FutureCores)]; //works
AllowedStates = LookupTable.FData[Integer(FutureCores)]; //invalid typecast
AllowedStates = LookupTable.FData[Word(FutureCores)]; //works, but not type safe.

关于arrays - 我可以使用集合类型作为数组索引吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46098375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com