Ada记录中的字符串数组

时间:2017-10-26 15:17:36

标签: arrays types ada

我在Ada代码中有以下记录:

type My_Type (Width, Height : Positive) is 
    record
        Data : array (1 .. Height) of String (1 .. Width);
        --  Some other stuff
    end record;

当然我的记录中不能有匿名数组,但我无法想出预先命名数组的方法。

我知道我可以让包依赖于宽度和高度来重新定义正确长度的字符串并命名它们的数组,但是这使得一次拥有许多不同大小的记录(我想要的)很笨拙。

有人知道这个问题有一个更优雅的解决方案吗?

注意:二维数组字符可以使用,但前提是我能以一种简单的方式从中提取字符串,这也是我不知道该怎么做的事情。

1 个答案:

答案 0 :(得分:4)

您的问题有几种可能的解决方案,包括使用无界字符串。以下是使用通用包的解决方案:

generic
   width : Positive;
   height : Positive;
package Gen_String_Matrix is
   Subtype My_String is String(1..width);
   type My_Matrix is array(1..height) of My_string;
end Gen_String_Matrix;

使用此软件包的一个示例是:

with Ada.Text_IO; use Ada.Text_IO;
with gen_string_matrix;

procedure Gen_String_Main is
   package My_Matrices is new Gen_String_Matrix(width  => 3,
                                                height => 10);
   use My_Matrices;
   Mat : My_Matrices.My_Matrix;
begin
   for Str of Mat loop
      Str := "XXX";
   end loop;
   for Str of Mat loop
      Put_Line(Str);
   end loop;
end Gen_String_Main;
相关问题