如何在 Ada 中为向量实现 Generic_Sorting?

How do you implement Generic_Sorting in Ada for a vector?

我正在尝试对很久以前的旧 C++ 代码进行一些基本的翻译以学习 Ada,但我一直对如何使用内置 Generic_Sorting 对向量进行排序感到困惑。我一直没能找到任何实际的具体例子,最接近的是一篇现已失效的丹麦语 wiki 文章,它看起来有一个完整的例子,但档案没有抢走它:https://web.archive.org/web/20100222010428/http://wiki.ada-dk.org/index.php/Ada.Containers.Vectors#Vectors.Generic_Sorting

这是我认为应该从上面开始工作的代码 link:

with Ada.Integer_Text_IO;    use Ada.Integer_Text_IO;
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Ada.Containers.Vectors; use Ada.Containers;

procedure Vectortest is
   package IntegerVector is new Vectors
     (Index_Type   => Natural,
      Element_Type => Integer);
   package IVSorter is new Generic_Sorting;

   IntVec : IntegerVector.Vector;
   Cursor : IntegerVector.Cursor;
begin
   IntVec.Append(3);
   IntVec.Append(43);
   IntVec.Append(34);
   IntVec.Append(8);

   IVSorter.Sort(Container => IntVec);

   Cursor := IntegerVector.First(Input);
   while IntegerVector.Has_Element(Cursor) loop
      Put(IntegerVector.Element(Cursor));
      IntegerVector.Next(Cursor);
   end loop;

end Vectortest;

我尝试了很多不同的 usewith 组合,但我只能得到各种错误代码。上面的代码给出了 Generic_Sorting is not visible,但是当我尝试显式声明 with Ada.Containers.Vectors.Generic_Sorting 时,我得到了错误 "Ada.Containers.Vectors.Generic_Sorting" is not a predefined library unit。我不知道我在这里做错了什么,我确信这是对 Ada 引入包的方式的根本误解,我希望确定这一点能帮助我更好地理解它。

Generic_SortingAda.Containers.Vectors 的内包,不能显式 withed(如您所见)。由于 Ada.Containers.Vectors 本身是一个通用包,因此 Generic_SortingAda.Containers.Vectors 实例化 的内部包。因此,您可以通过在实例名称前加上前缀来访问它:

package IVSorter is new IntegerVector.Generic_Sorting;