How do I make a 2D Vector in Ada?

Hello. I need to create a vector that holds vectors in Ada. I tried this code,which caused an error.

 package Vector_Vector is new Ada.Containers.Vectors(Index_Type => Positive, Element_Type => Ada.Containers.Vectors);
    use Vector_Vector;

What would be a functional way to do this?

You need to do it in two steps. So far you have tried to indicate a generic package as an element type.

To expand a bit on @zertovich, because you want Vectors with elements that are also Vectors, you have to make two instantiations of Ada.Containers.Vectors, one for the “inner” vectors (1-d vector) and one for the “outer” vector (the 2-d vector).

Also, if you just look at your example code with fresh eyes, you should see that it does not give the properties of the “inner” vector: what is the index type? what is the element type? To specify those properties, you need another instantiation of Ada.Containers.Vectors. For example:

   package One_D_Vec is new Ada.Containers.Vectors (
      Index_Type   => Positive,
      Element_Type => Float);

   package Vector_Vector is new Ada.Containers.Vectors (
      Index_Type   => Positive,
      Element_Type => One_D_Vec.Vector,
      "="          => One_D_Vec."=");
1 Like

In my G-NAV project I have built generic packages for 2D vectors and dynamic linked lists. They are quite straightforward to use. You can find this on GitHub GuillermoHazebrouck/G-NAV