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

ZSw*_*wat 4 ada gnat gnat-gps ada2012

我正在尝试从许多月前做一些旧C++代码的基本翻译来学习Ada,我一直对如何使用内置Generic_Sorting对矢量进行排序感到困惑.我还没有找到任何具体的实际例子,最接近现在已经不复存在的丹麦维基文章看起来好像有一个完整的例子,但存档没有抓住它:https:// web.archive.org/web/20100222010428/http://wiki.ada-dk.org/index.php/Ada.Containers.Vectors#Vectors.Generic_Sorting

这是我认为应该从上面的链接工作的代码:

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;
Run Code Online (Sandbox Code Playgroud)

我试过这么多不同的组合usewith,但所有我能得到的各种错误代码.上面的代码给出了Generic_Sorting is not visible,但是当我尝试明确说明with Ada.Containers.Vectors.Generic_Sorting我得到了错误"Ada.Containers.Vectors.Generic_Sorting" is not a predefined library unit.我不知道我在这里做错了什么,我确信这是对Ada带来的方式的一个根本误解,我希望把它钉在一边可以帮助我更好地理解它.

egi*_*lhh 10

Generic_Sorting是一个内部包,Ada.Containers.Vectors无法显式with编辑(正如您所发现的).而且因为Ada.Containers.Vectors它本身就是一个通用包,所以Generic_Sorting实例化的内包Ada.Containers.Vectors.因此,您可以通过在实例名称前添加前缀来访问它:

package IVSorter is new IntegerVector.Generic_Sorting;
Run Code Online (Sandbox Code Playgroud)

  • 就是这样,非常感谢你,我没有足够的代表来支持你,但我已经接受了解决方案,我不能告诉你我多么感激它!这一切都很有道理,我知道当我读到答案时我会面对面! (3认同)