Convert ada.Containers.Vector to array

ada, arrays, vector

Solution

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;

Problem

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: - Declare Temp_Arr as Integer_Array with capacity of V.Length - Iterate over V and copy all elements to Temp_Arr - 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?

Original source