Ada containers
last modified November 23, 2026
The generic packages in Ada.Containers provide resizable
collections without manual allocation and without giving up strong typing.
Each package is instantiated with the type of the elements, and the instance
contains its own vector, list, set, or map type. This article works through a
vector, a doubly linked list, an ordered set, an ordered map, and a hashed map,
and shows the cursors that select a position in each of them.
A vector
Ada.Containers.Vectors stores its elements in one resizable array
and gives them consecutive integer indices. The two formal parameters used here
are Index_Type, which selects the index range, and
Element_Type. Append adds at the end,
Length returns the number of elements, and Element
reads one by index.
with Ada.Text_IO;
with Ada.Containers.Vectors;
use Ada.Text_IO;
procedure Vector_Demo is
package Int_Vectors is new Ada.Containers.Vectors
(Index_Type => Positive,
Element_Type => Integer);
V : Int_Vectors.Vector;
begin
V.Append (10);
V.Append (20);
V.Append (30);
Put_Line ("length =" & Natural'Image (Natural (V.Length)));
Put_Line ("first =" & Integer'Image (V.First_Element));
Put_Line ("last =" & Integer'Image (V.Last_Element));
V.Replace_Element (2, 25);
Put_Line ("second =" & Integer'Image (V.Element (2)));
end Vector_Demo;
Length returns a value of type Count_Type, not
Natural, so the program converts it before applying
'Image. The first element is at index 1, which is the lower bound
of Positive. Replace_Element changes the value at
index 2 without moving any other element, and Element (2) shows
the result.
$ gnatmake -gnat2022 -gnatwa vector_demo.adb $ ./vector_demo length = 3 first = 10 last = 30 second = 25
Cursors and changes
A cursor designates a position in a container without exposing its
representation. First returns the first position,
Has_Element says whether a cursor still designates something, and
Next advances it. The element itself is read with
V (C), where C is the cursor. Insert
and Delete change the contents.
with Ada.Text_IO;
with Ada.Containers.Vectors;
use Ada.Text_IO;
procedure Vector_Cursor is
package Int_Vectors is new Ada.Containers.Vectors
(Index_Type => Positive,
Element_Type => Integer);
V : Int_Vectors.Vector;
begin
for I in 1 .. 4 loop
V.Append (I * 5);
end loop;
V.Insert (Before => 2, New_Item => 99);
V.Delete (4);
declare
C : Int_Vectors.Cursor := V.First;
begin
while Int_Vectors.Has_Element (C) loop
Put_Line ("item =" & Integer'Image (V (C)));
C := Int_Vectors.Next (C);
end loop;
end;
Put_Line ("length =" & Natural'Image (Natural (V.Length)));
end Vector_Cursor;
The vector starts as 5, 10, 15, 20. Inserting 99 before position 2 gives
5, 99, 10, 15, 20, and deleting position 4 removes 15. Writing
V.Insert (2, 99); would be rejected with "ambiguous call to
Insert", because the first parameter may be an index or a cursor; the named
association Before => 2 selects the index. Deletion invalidates
cursors that point at the removed element, but the deletion here happens
before the loop starts, so the cursor is created fresh.
$ gnatmake -gnat2022 -gnatwa vector_cursor.adb $ ./vector_cursor item = 5 item = 99 item = 10 item = 20 length = 4
A doubly linked list
Ada.Containers.Doubly_Linked_Lists stores each element in its own
node and keeps links in both directions. It has no index, so a position is
always a cursor. The element type must be definite: an unconstrained
String cannot be a list element, and an attempt to use it fails
with "unconstrained subtype in component declaration" and "actual for
Element_Type must be a definite subtype".
with Ada.Text_IO;
with Ada.Containers.Doubly_Linked_Lists;
with Ada.Strings.Unbounded;
use Ada.Text_IO;
procedure List_Names is
use Ada.Strings.Unbounded;
package Name_Lists is new Ada.Containers.Doubly_Linked_Lists
(Element_Type => Unbounded_String);
L : Name_Lists.List;
begin
L.Append (To_Unbounded_String ("Alice"));
L.Append (To_Unbounded_String ("Bob"));
L.Append (To_Unbounded_String ("Carol"));
L.Append (To_Unbounded_String ("Dave"));
declare
C : Name_Lists.Cursor := L.First;
Next : Name_Lists.Cursor;
begin
while Name_Lists.Has_Element (C) loop
Next := Name_Lists.Next (C);
if To_String (Name_Lists.Element (C)) = "Bob" then
Name_Lists.Delete (L, C);
end if;
C := Next;
end loop;
end;
for Name of L loop
Put_Line (To_String (Name));
end loop;
end List_Names;
Unbounded_String is a definite type whose objects can grow, so it
works where String does not. The deletion loop saves
Next (C) before it deletes C, because a deleted
cursor no longer designates a node. The final for ... of loop
visits the remaining elements in order and yields each one as an
Unbounded_String.
$ gnatmake -gnat2022 -gnatwa list_names.adb $ ./list_names Alice Carol Dave
An ordered set
Ada.Containers.Ordered_Sets keeps each value once and orders the
set with the "<" operator. Insert adds a value and
raises Constraint_Error when the value is already present, while
Include adds it only when it is absent and otherwise does
nothing. Contains answers whether a value is a member.
with Ada.Text_IO;
with Ada.Containers.Ordered_Sets;
use Ada.Text_IO;
procedure Set_Demo is
package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
S : Int_Sets.Set;
begin
S.Insert (30);
S.Insert (10);
S.Insert (20);
S.Include (10);
Put_Line ("length =" & Natural'Image (Natural (S.Length)));
Put_Line ("has 20 =" & Boolean'Image (S.Contains (20)));
Put_Line ("has 40 =" & Boolean'Image (S.Contains (40)));
for C in S.Iterate loop
Put_Line ("member =" & Integer'Image (Int_Sets.Element (C)));
end loop;
end Set_Demo;
The values are inserted in the order 30, 10, 20, but the iteration prints them
as 10, 20, 30, because the set is ordered. The second 10 is ignored by
Include, so the length stays three. Replacing
Include (10) with Insert (10) would raise
Constraint_Error with the message "attempt to insert element
already in set".
$ gnatmake -gnat2022 -gnatwa set_demo.adb $ ./set_demo length = 3 has 20 =TRUE has 40 =FALSE member = 10 member = 20 member = 30
An ordered map
Ada.Containers.Ordered_Maps associates a key with an element and
keeps the keys sorted. The key type must be definite, so the example uses
Unbounded_String keys and supplies the "<"
operator of that package. Include replaces the element of an
existing key, whereas Insert would raise on a duplicate.
with Ada.Text_IO;
with Ada.Containers.Ordered_Maps;
with Ada.Strings.Unbounded;
use Ada.Text_IO;
procedure Map_Demo is
use Ada.Strings.Unbounded;
package Price_Maps is new Ada.Containers.Ordered_Maps
(Key_Type => Unbounded_String,
Element_Type => Integer,
"<" => "<");
M : Price_Maps.Map;
begin
M.Insert (To_Unbounded_String ("apple"), 12);
M.Insert (To_Unbounded_String ("pear"), 8);
M.Insert (To_Unbounded_String ("plum"), 15);
M.Include (To_Unbounded_String ("apple"), 13);
Put_Line ("length =" & Natural'Image (Natural (M.Length)));
Put_Line ("apple =" & Integer'Image
(M.Element (To_Unbounded_String ("apple"))));
for C in M.Iterate loop
Put_Line (To_String (Price_Maps.Key (C)) & " =>"
& Integer'Image (Price_Maps.Element (C)));
end loop;
end Map_Demo;
The fourth call updates the value of apple from 12 to 13, so the
map has three entries rather than four. The iteration prints the keys in
alphabetical order, because the ordered map sorts them with
"<". A lookup with M.Element (Key) raises
Constraint_Error when the key is absent; Find
returns a cursor that can be tested with Has_Element instead.
$ gnatmake -gnat2022 -gnatwa map_demo.adb $ ./map_demo length = 3 apple = 13 apple => 13 pear => 8 plum => 15
A hashed map
Ada.Containers.Hashed_Maps does not order its keys. Instead it
asks for a hash function and an equality function. The hash function below
converts the key to a String and calls
Ada.Strings.Hash; the result has type
Ada.Containers.Hash_Type. Find returns a cursor, and
Replace_Element updates the value behind it.
with Ada.Text_IO;
with Ada.Containers;
with Ada.Containers.Hashed_Maps;
with Ada.Strings.Hash;
with Ada.Strings.Unbounded;
use Ada.Text_IO;
procedure Hmap_Demo is
use Ada.Strings.Unbounded;
function Word_Hash (Key : Unbounded_String)
return Ada.Containers.Hash_Type is
begin
return Ada.Strings.Hash (To_String (Key));
end Word_Hash;
package Word_Maps is new Ada.Containers.Hashed_Maps
(Key_Type => Unbounded_String,
Element_Type => Natural,
Hash => Word_Hash,
Equivalent_Keys => "=");
M : Word_Maps.Map;
procedure Add (W : String) is
Key : constant Unbounded_String := To_Unbounded_String (W);
C : constant Word_Maps.Cursor := M.Find (Key);
begin
if Word_Maps.Has_Element (C) then
M.Replace_Element (C, Word_Maps.Element (C) + 1);
else
M.Insert (Key, 1);
end if;
end Add;
Words : constant array (1 .. 6) of String (1 .. 5) :=
["apple", "grape", "melon", "apple", "grape", "apple"];
begin
for W of Words loop
Add (W);
end loop;
Put_Line ("distinct =" & Natural'Image (Natural (M.Length)));
Put_Line ("apple =" & Natural'Image
(M.Element (To_Unbounded_String ("apple"))));
Put_Line ("melon =" & Natural'Image
(M.Element (To_Unbounded_String ("melon"))));
end Hmap_Demo;
The word array has six entries, but the map has three keys. Add
finds an existing key with Find, increments its value, and inserts
a new key with count 1 otherwise. The iteration order of a hashed map is not
defined, so the program asks for the counts of particular keys instead of
printing the map in order. The result shows three apples, two grapes, and one
melon.
$ gnatmake -gnat2022 -gnatwa hmap_demo.adb $ ./hmap_demo distinct = 3 apple = 3 melon = 1
Next steps
The next article leaves containers behind and introduces tagged types, which carry a run-time tag and can be extended with new components. It shows primitive operations, dot notation, and type extension, and prepares the ground for inheritance and dispatching.
Source
In this article we have used a vector with indexed access and cursors, a doubly linked list with deletion during iteration, an ordered set, an ordered map, and a hashed map with a custom hash function. The generics article explains the instantiation syntax that every container package uses.
Author
List all Ada tutorials.