gpt4 book ai didi

arrays - 字符串文字的 Ada 常量数组

转载 作者:行者123 更新时间:2023-12-02 00:49:30 26 4
gpt4 key购买 nike

我有一个大型 C 数组,我希望将其移入我的 ada 项目中。该数组用于存储稍后将加载的 Assets 的文件名。它看起来像:

const char *filenames[NUMBER_OF_FILES] = {
/* file[0] */ "res/0.file",
/* ... */
/* file[n] */ "res/some_more/complicated/file.name"
};

我想把它移到 ada 包体中,但找不到合适的方法。显然我的第一次尝试是:

filenames : constant array (File_Index) of String := (
index_0 => "res/0.file",
-- ...
index_n => "res/some_more/complicated/file.name"
);

当然,String 是一种不受约束的类型,因此 Ada 不允许这样做。我将其切换为使用 Unbounded_Strings,它可以工作,但非常难看(必须用 To_Unbounded_String 包装每个字符串。

有什么方法可以像这样创建一个大小在编译时已知的无约束类型数组,还是我必须使用无界字符串?

最佳答案

它有点低级和重复,但也许你可以创建一个小程序(甚至可以在 Ada 中!)来生成类似

with Ada.Text_IO; use Ada.Text_IO;
procedure Lambdabeta is
F1 : aliased constant String := "res/0.file";
F2 : aliased constant String := "res/some_more/complicated/file.name";
type Strings is array (Positive range <>) of access constant String;
Filenames : constant Strings := (F1'Access,
F2'Access);
begin
for J in Filenames'Range loop
Put_Line (Filenames (J).all);
end loop;
end Lambdabeta;

另见 this answer关于最大限度地减少使用 To_Unbounded_String 的痛苦。

关于arrays - 字符串文字的 Ada 常量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41151494/

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