在Ada中创建一个空数组

问题描述

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

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

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

arr2:Integer的array(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;

解决方法

在Ada L中将

L : array (1 .. N,1 .. 0) of Integer;

但是它没有用,因为您以后将无法对其进行扩展。 @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;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...