在 Ada 中创建一个空数组数组

Yel*_*t11 4 arrays ada arrayofarrays

我正在尝试在 Python 中编写与以下语句等效的 Ada:L = [[] for i in range(n)]

我正在解决一个动态规划问题,我的计划是如果第 i 个数组的元素少于第 j 个数组的元素,则最终将 L 中第 j 个数组的内容复制到第 i 个数组中(j < i)。

我已经找到了如何通过以相反的顺序定义其范围来创建一个空数组。因此,例如, arr2 将是一个如下创建的空数组:

arr2:整数数组(2 .. 1);

我的问题是,如何定义更大的数组 L 以包含 n 个这样的 arr2 数组?

请告诉我。

更新:我能够使用下面的答案使其正常工作。这是我的代码。

package Integer_Vectors is new Ada.Containers.Vectors
    (Index_Type   => Natural,
    Element_Type => Integer);

    N: Integer;

    Array_Of_Vectors : array(1 .. N) of Integer_Vectors.Vector := (others => Integer_Vectors.Empty_Vector);

    Input_Sequence: Integer_Vectors.Vector;

    Max: Integer_Vectors.Vector;

    Input_List : String := Get_Line;

    IntCast : Integer;

    Last : Positive := 1;
    
    begin

    while Last < Input_List'Last loop
        Get(Input_List(Last..Input_List'Last),IntCast,Last);
        Input_Sequence.Append(IntCast);
        Last := Last + 1;
    end loop;
    
    N := 0;

    for i of Input_Sequence loop
        N := N + 1;
    end loop;
Run Code Online (Sandbox Code Playgroud)

Max*_*nik 6

在 Ada L 中

L : array (1 .. N, 1 .. 0) of Integer;
Run Code Online (Sandbox Code Playgroud)

但它没有用,因为您将无法在以后扩展它。@Zerte 是对的。

例如,您可以在不定元素上使用向量。像这样

with Ada.Containers.Indefinite_Vectors;

type Integer_Array is array (Positive range <>) of Integer;

Empty : constant Integer_Array := (1 .. 0 => <>);

package Integer_Array_Vectors is new
 Ada.Containers.Indefinite_Vectors
   (Index_Type => Positive, Element_Type => Integer_Array);

L : Integer_Array_Vectors.Vector;

L.Append (Empty, N);

if L (J)'Length > L (I)'Length then
   L.Replace_Element (I, L(J));
end if;
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,在这里使用不定向量似乎有点矫枉过正。n(L 的大小)是已知且固定的:L 是一个数组。L 中的数组具有不同的长度:它们是向量。向量可以在数组内使用。另一个解决方案可能是“L : array(1 .. n) of Integer_Vectors.Vector := (others =&gt; Integer_Vectors.Empty_Vector);” (2认同)