我有许多常量数组,但它们并不都具有相同数量的元素。
为了存储这些数组,我声明了一个足够大的数组类型来存储(或引用?)这些数组中最大的每个元素:
type
TElements = array [1 .. 1024] of Single;
这些 TElements 数组中的每一个都在逻辑上与具有相同元素数量的另一个 TElements 数组相关联。
因此,为了将这些大小相等的数组配对,我将记录类型声明为:
type
TPair = record
n : Integer; // number of elements in both TElements arrays
x : ^TElements;
y : ^TElements;
end;
然后我定义包含常量 TElements 数组对的常量 TPair 记录:
const
ElementsA1 : array [1 .. 3] of Single = (0.0, 1.0, 2.0);
ElementsA2 : array [1 .. 3] of Single = (0.0, 10.0, 100.0);
ElementsA : TPair =
(
n : 3;
x : @ElementsA1;
y : @ElementsA2;
);
ElementsB1 : array [1 .. 4] of Single = (0.0, 1.0, 2.0, 3.0);
ElementsB2 : array [1 .. 4] of Single = (0.0, 10.0, 100.0, 1000.0);
ElementsB : TPair =
(
n : 4;
x : @ElementsB1;
y : @ElementsB2;
);
<小时/>
这似乎是引用数组数据的低效方法(也许不是,我不知道)。
我想维护一个包含两个常量数组的常量数据类型(“对”数据类型)。
在每个“对”中,两个数组保证具有相同数量的元素。
但是,不能保证一个“对”中的数组元素数量等于任何其他“对”中的数组元素数量。
有没有办法声明常量“对”数据类型,以便包含的数组大小由常量数组定义确定?
理想情况下,我想摆脱 TElements 类型和尴尬的指针。如果能编译的话,这样的东西会很酷:
type
TPair = record
x : array of Single;
y : array of Single;
end;
const
ElementsA : TPair =
(
x : (0.0, 1.0, 2.0);
y : (0.0, 10.0, 100.0);
);
ElementsB : TPair =
(
x : (0.0, 1.0, 2.0, 3.0);
y : (0.0, 10.0, 100.0, 1000.0);
);
但我猜由于数组被声明为动态数组,它不想在运行时之前为它们分配内存?
最佳答案
Is there a way to declare a constant "pair" data type so that the contained array sizes are determined by the constant array definition?
不,遗憾的是这是不可能的。您必须在方括号内声明数组的大小。
关于arrays - Delphi:声明包含常量数组的常量记录类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8187952/