Differences

This shows you the differences between two versions of the page.

Link to this comparison view

vectors.insert_space [2012/01/23 21:10] (current)
thomaslocke created
Line 1: Line 1:
 +<code ada>
 +with Ada.Text_IO;​
 +with Ada.Text_IO.Unbounded_IO;​
 +with Ada.Strings.Unbounded; ​     use Ada.Strings.Unbounded;​
 +with Ada.Containers.Vectors; ​    use Ada.Containers;​
 +
 +procedure Quotes is
 +   ​package IO renames Ada.Text_IO;​
 +   ​package SUIO renames Ada.Text_IO.Unbounded_IO;​
 +   ​package Quote_Container is new Vectors (Natural, Unbounded_String);​
 +   use Quote_Container;​
 +   ​Quotes ​   : Vector;
 +   ​Input ​    : IO.File_Type;​
 +   ​Q_Cursor ​ : Cursor; ​ --  Declare the cursor.
 +begin
 +   ​IO.Open (File => Input,
 +            Mode => IO.In_File,
 +            Name => "​quotes.txt"​);​
 +   while not IO.End_Of_File (File => Input) loop
 +      Quotes.Append (New_Item => SUIO.Get_Line (File => Input));
 +   end loop;
 +   ​IO.Close (Input);
 +   
 +   ​-- ​ Output all the quotes prior to any Insert_Space calls
 +   for i in Quotes.First_Index .. Quotes.Last_Index loop
 +      SUIO.Put_Line (Item => i'Img & " " & Quotes.Element (i));
 +   end loop;
 + 
 +   ​IO.New_Line (2);
 + 
 +   ​-- ​ Insert two "​empty"​ elements at the beginning of the vector
 +   ​Quotes.Insert_Space (Before => 0,
 +                        Count => 2);
 + 
 +   ​-- ​ Insert 3 "​empty"​ elements before index 6 in the vector
 +   ​Quotes.Insert_Space (Before => 5,
 +                        Count  => 3);
 + 
 +   ​-- ​ Insert 3 "​empty"​ elements before the last element, using a cursor.
 +   ​Q_Cursor := Quotes.To_Cursor (Index => Quotes.Last_Index);​
 +   ​Quotes.Insert_Space (Before ​  => Q_Cursor,
 +                        Position => Q_Cursor,
 +                        Count    => 3);
 + 
 +   for i in Quotes.First_Index .. Quotes.Last_Index loop
 +      SUIO.Put_Line (Item => i'Img & " " & Quotes.Element (i));
 +   end loop;
 +end Quotes;
 +</​code>​
 +
 +<code ada>
 +with Ada.Text_IO;​
 +with Ada.Text_IO.Unbounded_IO;​
 +with Ada.Strings.Unbounded; ​     use Ada.Strings.Unbounded;​
 +with Ada.Containers.Vectors; ​    use Ada.Containers;​
 +
 +procedure Quotes is
 +   ​package IO renames Ada.Text_IO;​
 +   ​package SUIO renames Ada.Text_IO.Unbounded_IO;​
 +   ​package Quote_Container is new Vectors (Natural, Unbounded_String);​
 +   use Quote_Container;​
 +   ​Quotes ​   : Vector;
 +   ​Input ​    : IO.File_Type;​
 +   ​Q_Cursor ​ : Cursor; ​ --  Declare the cursor.
 +begin
 +   ​IO.Open (File => Input,
 +            Mode => IO.In_File,
 +            Name => "​quotes.txt"​);​
 +   while not IO.End_Of_File (File => Input) loop
 +      Quotes.Append (New_Item => SUIO.Get_Line (File => Input));
 +   end loop;
 +   ​IO.Close (Input);
 +   
 +   ​-- ​ Output all the quotes prior to any Insert_Space calls
 +   for i in Quotes.First_Index .. Quotes.Last_Index loop
 +      SUIO.Put_Line (Item => i'Img & " " & Quotes.Element (i));
 +   end loop;
 + 
 +   ​IO.New_Line (2);
 + 
 +   ​-- ​ Insert two "​empty"​ elements at the beginning of the vector
 +   ​Quotes.Insert_Space (Before => 0,
 +                        Count => 2);
 + 
 +   ​-- ​ We now insert some actual data in the two new elements
 +   ​Quotes.Replace_Element (Index ​   => 0,
 +                           ​New_Item => To_Unbounded_String ("New index 0"));
 +   ​Quotes.Replace_Element (Index ​   => 1,
 +                           ​New_Item => To_Unbounded_String ("New index 1"));
 + 
 +   ​-- ​ Insert 3 "​empty"​ elements before index 5 in the vector
 +   ​Quotes.Insert_Space (Before => 5,
 +                        Count  => 3);
 + 
 +   ​-- ​ We now insert some actual data in the three new elements
 +   ​Quotes.Replace_Element (Index ​   => 5,
 +                           ​New_Item => To_Unbounded_String ("New index 5"));
 +   ​Quotes.Replace_Element (Index ​   => 6,
 +                           ​New_Item => To_Unbounded_String ("New index 6"));
 +   ​Quotes.Replace_Element (Index ​   => 7,
 +                           ​New_Item => To_Unbounded_String ("New index 7"));
 + 
 +   ​-- ​ Insert 3 "​empty"​ elements before the last element, using a cursor.
 +   ​Q_Cursor := Quotes.To_Cursor (Index => Quotes.Last_Index);​
 +   ​Quotes.Insert_Space (Before ​  => Q_Cursor,
 +                        Position => Q_Cursor,
 +                        Count    => 3);
 + 
 +   ​-- ​ We now insert some actual data in the last three new elements
 +   ​Quotes.Replace_Element (Index ​   => 14,
 +                           ​New_Item => To_Unbounded_String ("New index 14"));
 +   ​Quotes.Replace_Element (Index ​   => 15,
 +                           ​New_Item => To_Unbounded_String ("New index 15"));
 +   ​Quotes.Replace_Element (Index ​   => 16,
 +                           ​New_Item => To_Unbounded_String ("New index 16"));
 + 
 +   for i in Quotes.First_Index .. Quotes.Last_Index loop
 +      SUIO.Put_Line (Item => i'Img & " " & Quotes.Element (i));
 +   end loop;
 +end Quotes;
 +</​code>​
 +
 +[[Ada.Containers.Vectors#​Vectors.Insert_Space | Go back]]
  

Navigation