在Ada中,如果我使用带有哈希映射中已存在的键的insert命令,它是否只更新存储在该键中的值?以下是该Hashed_Maps软件包的Ada 2005规范:http:
//www.adaic.org/resources/add_content/standards/05rm/html/RM-A-18-5.html
谢谢
这个答案是正确的,但它确实取决于Insert你使用的是什么.有几个(ARM A.18.4(44ff)); 如果你使用简单的
procedure Insert (Container : in out Map;
Key : in Key_Type;
New_Item : in Element_Type);
Run Code Online (Sandbox Code Playgroud)
然后呢
根据五参数Insert将Key和New_Item插入Container,区别在于如果具有等效于Key的键的节点已经在Map中,则传播Constraint_Error.
在阅读所Insert提供的程序时Hashed_Maps,还请参阅所有人共同的子程序文档Maps.特别注意out类型的参数Boolean:
如果找到匹配项,
Inserted则设置为False并Position使用匹配键指定元素.否则,Insert分配一个新节点,将其初始化为Key和New_Item,并将其添加到Container;Inserted设置为True并Position指定新插入的节点.
之后Insert,现有的键/项对将保持不变,但是Replace如果您的用例需要,您可以使用相应的键项,例如增加特定键遇到的次数.
在该相关示例中,该过程Read_Dictionary将每个字典单词(键)映射到单词集(项目).在循环中,过程检查Inserted以确定是否应更新新的或现有的集.
Word_Map.Insert(Sorted, Position, Inserted);
if Inserted then
Set := new ACOS.Set;
Word_Map.Replace_Element(Position, Set);
else
Set := ACHM.Element(Position);
end if;
Set.Insert(Word);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |