Recently I decided to learn Ada by doing a few problems from the Project Euler. The first two problems were pretty straightforward when it came to the implementation. The third problem was a little more complicated because the solution that I choose to implement required the use of a vector as I did not know the number of elements that would need stored in an array before run-time. It turns out this was more difficult than I had imagined because Google either failed to turn up any results for 'vector ada example' that gave an example of how to actually use them or the internet did not have any articles covering this. So this post will remedy that and give the internet at least one example of how to use vectors in Ada.

First we have to use the `Ada.Containers.Vectors`. Open up a file titled `euler3.adb` and add the following statements:

``````with Ada.Containers.Vectors;

procedure Euler3 is

package Integer_Vectors is new Vectors(Natural, Integer);

begin

end
``````

The line `package Integer_Vectors is new Vectors(Natural, Integer);` states that the package `Integer_Vectors` is a vector with the indexs being natural numbers and the values being integers.

Ok, now lets make the program actually do something:

``````with Ada.Containers.Vectors;

procedure Euler3 is

package Integer_Vectors is new Vectors(Natural, Integer);

Numbers : Integer_Vectors.Vector;

begin

Numbers.Append(43);

end
``````

Go ahead and compile with GNAT: `gnat make euler3.adb`. After running the program it will out put `43`.

Note that the operations on the vector can either but done using the Object Oriented notation or the traditional:

``````with Ada.Containers.Vectors;

procedure Euler3 is

package Integer_Vectors is new Vectors(Natural, Integer);

Numbers : Integer_Vectors.Vector;

begin

Integer_Vectors.Append(Numbers, 43);

end Euler3;
``````

Compile and run and the output will be the same.

For more useful functions and procedures see the Ada Reference Manual.

But there is one more thing that I would like to touch on, Cursors. Some languages offer easy iteration. Ruby for example allows nice concise syntax: `[1,2,3].each { |x| puts x }`. Others are not so easy.

``````with Ada.Containers.Vectors;

procedure Euler3 is

package Integer_Vectors is new Vectors(Natural, Integer);

Numbers : Integer_Vectors.Vector;
Cursor : Integer_Vectors.Cursor;

begin

-- Get some content
Integer_Vectors.Append(Numbers, 43);
Integer_Vectors.Append(Numbers, 44);
Integer_Vectors.Append(Numbers, 45);

Cursor := Integer_Vectors.First(Numbers);
while Integer_Vectors.Has_Element(Cursor) loop