Ada: Standard Library

The Ada standard defines the library with the language — Ada.* namespaces are portable across every compiler. This tour covers the three families you will use constantly: containers, numerics, and the string/character utilities.

Containers

Ada.Containers provides generic, type-safe collections. They are formal packages — you instantiate them for your element type, and every operation is checked:

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

procedure Container_Demo is
   --  a vector of unbounded strings: Positive is the index subtype
   package Name_Vectors is new Ada.Containers.Vectors
     (Index_Type => Positive, Element_Type => Unbounded_String);
   use Name_Vectors;

   Names : Vector;
begin
   Names.Append (To_Unbounded_String ("Ada"));
   Names.Append (To_Unbounded_String ("Jean"));
   Names.Append (To_Unbounded_String ("Tucker"));

   --  iterate with a cursor or a plain loop over the index range
   for I in Names.First_Index .. Names.Last_Index loop
      Put_Line (To_String (Names (I)));
   end loop;

   Put_Line (Natural'Image (Length (Names)) & " names");
end Container_Demo;

The family covers the standard structures — all with the same instantiate-then-use pattern:

ContainerPackageNotes
VectorAda.Containers.Vectorsgrowable array, O(1) index
Doubly linked listAda.Containers.Doubly_Linked_Listsstable cursors, O(1) splice
Hashed mapAda.Containers.Hashed_Mapskey → element, user hash
Ordered mapAda.Containers.Ordered_Mapssorted traversal, no hash needed
SetsHashed_Sets, Ordered_Setsmembership, set algebra
QueuesUnbounded_Priority_Queuestasking-ready priority queues

Cursors (Container.First, Next (C)) traverse without invalidating on append for lists; vectors invalidate on reallocation — the docs state which operations invalidate, precisely.