首页 > 解决方案 > 从有序记录集中删除一个元素 Ada

问题描述

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

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;

现在我想访问一个具有卷号 5 的元素并将其删除。

    Student_Store.Delete(Student_Details.Roll_Number => 5)

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

另请注意,如果记录是元素,则在初始化时如何定义对有序集进行排序的键。

标签: containersada

解决方案


你确定你的数据结构应该是一个集合吗?你想处理它的方式,我的印象是它应该是一个以地图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;

推荐阅读