簡體   English   中英

在Ada中,可以在陣列初始化時使用“其他”來表示不同的值嗎?

[英]In Ada is it possible to use 'others' on array initialisation for different values?

我想知道是否可以初始化這個數組:

Type Some is array (1..5) of Integer;

SomeArray : Some := ( 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 );

以某種不同的方式,像這樣:

SomeArray : Some := ( others => n );

可能嗎?

除了“某些”現在是Ada 2012中的保留字這一事實外,您可以完全按照您的描述初始化這種數組。 例如:

with Text_IO; use Text_IO;

procedure Agg_Init_Test is

   type Some_Type is array (1.. 5) of Integer;

   N : constant := 4;

   Data : Some_Type := (others => N);

   procedure Init_To (N : Integer) is
   begin
      Data := (others => N);
   end Init_To;

   procedure Init_Data (Data : out Some_Type; N : Integer) is
   begin
      Data := (others => N);
   end Init_Data;

   function Inc (Val : in out Integer) return Integer is
   begin
      Val := Val + 1;
      return Val;
   end Inc;

   procedure Init_Seq(Data : out Some_Type; Start : Integer) is
      N : Integer := Start;
   begin
      Data := (others => Inc(N));
   end Init_Seq;

begin
   Init_To(42);
   Init_Data(Data, 2012);
   Init_Seq(Data, 0);
   for I of Data loop
      Put_Line(Integer'Image(I));
   end loop;
end Agg_Init_Test;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM