b00dle
b00dle

Reputation: 1528

Convert ada.Containers.Vector to array

If I have defined an array type like

type Integer_Array is array(Natural range <>) of Integer;

and also use package Ada.Containers.Vectors as

package Integer_Vectors is new Ada.Containers.Vectors(
    Element_Type => Integer,
    Index_Type => Natural);
use Integer_Vectors;

How can I implement the following function?

function To_Integer_Array(V : Integer_Vectors.Vector) return Integer_Array;

What I have so far

Conceptually, it seems really easy:

  1. Declare Temp_Arr as Integer_Array with capacity of V.Length
  2. Iterate over V and copy all elements to Temp_Arr
  3. Return Temp_Arr

Step 1. is giving me headaches though. I have tried:

function To_Integer_Array(V: Integer_Vectors.Vector) return Integer_Array is
    Temp_Arr: Integer_Array(1..V.Length);
begin
    -- Todo: copy values here
    return Temp_Arr;
end To_Integer_Array;

This will throw

expected type "Standard.Integer"
found type "Ada.Containers.Count_Type"

While the error absolutely makes sense, I am unsure as to how I might solve it.

Is there a way to cast Ada.Containers.Count_Type to Standard.Integer? Would there be another way to create an Integer_Array from Integer_Vector?

Upvotes: 3

Views: 1033

Answers (1)

b00dle
b00dle

Reputation: 1528

Thanks to Brian the declaration now works. The correct implementation for my function looks like this:

function To_Integer_Array(V: Integer_Vector) return Integer_Array is
    Temp_Arr: Integer_Array(1..Natural(V.Length));
begin
    for I in Temp_Arr'Range loop
        Temp_Arr(I) := V.Element(I);
    end loop;
    return Temp_Arr;
end To_Integer_Array;

Upvotes: 5

Related Questions