微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Ada中创建一个空数组

如何解决在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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。