从有序记录Ada中删除元素

时间:2018-07-27 10:22:12

标签: containers ada

我在Ada中构造了一组有序的记录。

batch_size=4

现在,我要访问滚动编号为5的元素并将其删除。

with Ada.Containers.Ordered_Sets;

procedure Demonstrator is

   type Streams is (Mech);
   type Gend is (Female, Male);

   type Student_Details is record
      Roll_Number    : Positive range 1 .. 100;
      Name           : String (1 .. 5);
      Age            : Natural range 18 .. 40;
      DOB            : String (1 .. 8);
      Department     : Streams;
      Admission_Date : String (1 .. 8);
      Pursuing_Year  : Integer range 2010 .. 2099;
      Gender         : Gend;
   end record;

   function "<" (Left, Right : Student_Details) return Boolean is
   begin
      return Left.Roll_Number < Right.Roll_Number;
   end "<";

   package Student_Sets is
     new Ada.Containers.Ordered_Sets (Element_Type => Student_Details);
   use Student_Sets;

   Student_Store : Student_Sets.Set;
begin
   Student_Store.Insert ((2, "iuytr", 19, "28031989", MECH, "26072018", 2018, Male));
   Student_Store.Insert ((4, "cobol", 19, "28031989", MECH, "26072018", 2018, Male));
   Student_Store.Insert ((3, "sdfsd", 19, "28031989", MECH, "26072018", 2018, Male));
   Student_Store.Insert ((5, "sfdff", 19, "28031989", MECH, "26072018", 2018, Male));
end Demonstrator;

无法删除它。 您能否通过使用特定键引用成员来帮助删除该成员。

另外要注意的是,当记录是元素时,如何在初始化时定义键来排序有序集。

1 个答案:

答案 0 :(得分:4)

您确定您的数据结构应该是集合吗?您想要使用它的方式,给人的印象是它应该是Roll_Number作为键的映射。

如果它确实应该是一个集合,那么@egilhh的注释为我们指明了正确的方向:

with Ada.Containers.Ordered_Sets;

procedure Demonstrator is

   type Streams is (Mech);
   type Gend is (Male);

   type Student_Details is record
      Roll_Number    : Positive range 1 .. 100;
      Name           : String (1 .. 5);
      Age            : Natural range 18 .. 40;
      DOB            : String (1 .. 8);
      Department     : Streams;
      Admission_Date : String (1 .. 8);
      Pursuing_Year  : Integer range 2010 .. 2099;
      Gender         : Gend;
   end record;

   function "<" (Left, Right : Student_Details) return Boolean is
   begin
      return Left.Roll_Number < Right.Roll_Number;
   end "<";

   function Roll_Number (Item : in Student_Details) return Positive is
     (Item.Roll_Number);

   package Student_Sets is
     new Ada.Containers.Ordered_Sets (Element_Type => Student_Details);

   package Roll_Numbers is
     new Student_Sets.Generic_Keys (Key_Type => Positive,
                                    Key      => Roll_Number);

   use all type Student_Sets.Set;

   Student_Store : Student_Sets.Set;
begin
   Student_Store.Insert ((2, "iuytr", 19, "28031989", MECH, "26072018", 2018, Male));
   Student_Store.Insert ((4, "cobol", 19, "28031989", MECH, "26072018", 2018, Male));
   Student_Store.Insert ((3, "sdfsd", 19, "28031989", MECH, "26072018", 2018, Male));
   Student_Store.Insert ((5, "sfdff", 19, "28031989", MECH, "26072018", 2018, Male));

   Roll_Numbers.Delete (Container => Student_Store,
                        Key       => 5);
end Demonstrator;