containers - 从有序记录集中删除一个元素 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)
无法删除它。您能否通过使用特定键引用成员来帮助删除成员。
另请注意,如果记录是元素,则在初始化时如何定义对有序集进行排序的键。
解决方案
你确定你的数据结构应该是一个集合吗?你想处理它的方式,我的印象是它应该是一个以地图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;
推荐阅读
- python-3.x - 将约束与 PULP 中的目标相关联
- angular - RxJS 更新状态和传递
- java - javax.xml.transform.Transformer 行尾不再尊重系统属性“line.separator”
- javascript - 根据javascript中的一些值过滤JSON
- sql-server - SSRS(2016)中的时间流程图?
- webpagetest - 了解 WebPageTest 瀑布视图中的颜色
- c# - 有效负载中的属性文件夹的值与架构 Graph Api 不匹配
- python - Python:如何在每次增量迭代后附加在嵌套循环中创建的数据
- kotlin - 在 Kotlin 中创建全局常量的正确方法是什么?
- algorithm - 使用带有 2 个参数的谓词进行过滤