如何在 Ada 中定义字符串数组?

Mac*_*lva 3 arrays string warnings ada

我想要的是在 Ada 中定义一个字符串数组。

我正在尝试执行这段代码:

type String is array (Positive range <>) of Character;
type lexicon is array(1..7) of String(1..20);
nomFumadors : lexicon := ("Macia","Xisco","Toni","Laura","Rocky","Paz");
nomNoFumadors : lexicon := ("Marina","Marta","Joan","Africa","America");
Run Code Online (Sandbox Code Playgroud)

编译器说:

warning:wrong length for array of subtype of "String" defined at line 42
Run Code Online (Sandbox Code Playgroud)

我的第 42 行是这样的:

type lexicon is array(1..7) of String(1..20);
Run Code Online (Sandbox Code Playgroud)

但编译器说警告位于第 43 和 44 行:这些是什么:

nomFumadors : lexicon := ("Macia","Xisco","Toni","Laura","Rocky","Paz");
nomNoFumadors : lexicon := ("Marina","Marta","Joan","Africa","America");
Run Code Online (Sandbox Code Playgroud)

有人可以帮我吗?

Jer*_*ere 6

其他人提到了有界和无界字符串。您还可以使用 Indefinite_Vectors。您可以使用“&”运算符来初始化它们(与初始值设定项列表相反,尽管 Ada 的下一版本正在向容器添加初始值设定项列表)。您可以像数组一样使用向量,通过传递索引,此外您还可以获得许多其他附加功能。

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Indefinite_Vectors;

procedure Hello is

    package Vectors is new Ada.Containers.Indefinite_Vectors
        (Index_Type   => Positive,
         Element_Type => String);
         
    use type Vectors.Vector;
         
    nomFumadors   : Vectors.Vector 
        := Vectors.Empty_Vector 
            & "Macia" 
            & "Xisco" 
            & "Toni" 
            & "Laura" 
            & "Rocky" 
            & "Paz";
    nomNoFumadors : Vectors.Vector
        := Vectors.Empty_Vector 
            & "Marina" 
            & "Marta" 
            & "Joan" 
            & "Africa" 
            & "America";
begin
    Put_Line("Hello, world!");
    
    -- Loop through Elements
    for Name of nomFumadors loop
        Put_Line(Name);
    end loop;
    
    -- Loop by index
    for Index in nomNoFumadors.Iterate loop
        Put_Line(nomNoFumadors(Index));
    end loop;
    
end Hello;
Run Code Online (Sandbox Code Playgroud)


fly*_*lyx 5

您声明数组保存长度为 20 的字符串。您给出的字符串文字长度小于 20 个字符。因此出现了错误。

您似乎正在寻找最多包含20个字符的字符串类型。这在以下内容中提供Ada.Strings.Bounded

package Max_20_String is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
use Max_20_String;

type Lexicon is array (1..7) of Bounded_String; -- from Max_20_String
nomFumadors : Lexicon := (To_Bounded_String ("Macia"),
                          To_Bounded_String ("Xisco"),
                          To_Bounded_String ("Toni"),
                          To_Bounded_String ("Laura"),
                          To_Bounded_String ("Rocky"),
                          To_Bounded_String ("Paz"));
Run Code Online (Sandbox Code Playgroud)

要从 Bounded_String 返回 String,请使用例如To_String (Lexicon (2))


Zer*_*rte 5

另一种选择是Unbounded_String(顾名思义,长度是可变且无限制的):

with Ada.Strings.Unbounded;

procedure Fumador is
  use Ada.Strings.Unbounded;

  subtype VString is Unbounded_String;
  function "+" (Source : in String) return VString renames To_Unbounded_String;

  type Lexicon is array (Integer range <>) of VString;  --  Unknown number of people.
  nomFumadors : Lexicon := (+"Macia", +"Xisco", +"Toni", +"Laura", +"Rocky", +"Paz");
  nomNoFumadors : Lexicon := (+"Marina", +"Marta", +"Joan", +"Africa", +"America");
  
begin
  null;
end;
Run Code Online (Sandbox Code Playgroud)