gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:11:11 24 4
gpt4 key购买 nike

我正在尝试在 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;

最佳答案

在 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;

关于arrays - 在 Ada 中创建一个空数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63821649/

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