从有序记录集 Ada 中删除一个元素
Delete an element from an ordered set of records 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;
现在我想访问一个 Roll 编号为 5 的元素并将其删除。
Student_Store.Delete(Student_Details.Roll_Number => 5)
无法删除。
您能否通过使用特定键引用成员来帮助删除该成员。
另外在一个单独的注释中,如果记录是一个元素,如何在初始化时定义对有序集进行排序的键。
你确定你的数据结构应该是一个集合吗?你想要对待它的方式,我得到的印象是它应该是一个以 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;
我已经在 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;
现在我想访问一个 Roll 编号为 5 的元素并将其删除。
Student_Store.Delete(Student_Details.Roll_Number => 5)
无法删除。 您能否通过使用特定键引用成员来帮助删除该成员。
另外在一个单独的注释中,如果记录是一个元素,如何在初始化时定义对有序集进行排序的键。
你确定你的数据结构应该是一个集合吗?你想要对待它的方式,我得到的印象是它应该是一个以 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;