Differences

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

Link to this comparison view

vectors.insert [2012/01/23 21:08] (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;​
 +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);
 +   
 +   ​-- ​ Insert an element at the second index of the vector
 +   ​Quotes.Insert (Before ​  => 1,
 +                  New_Item => To_Unbounded_String ("New second index!"​));​
 + 
 +   ​-- ​ Insert two identical elements at the fifth index of the vector
 +   ​Quotes.Insert (Before ​  => 4,
 +                  New_Item => To_Unbounded_String ​
 +                    ("Two new elements @ 4 and 5!"),
 +                  Count    => 2);
 + 
 +   ​-- ​ Insert two empty elements before the last index of the vector
 +   ​Quotes.Insert (Before => Quotes.Last_Index,​
 +                  Count  => 2);
 + 
 +   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;
 +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);
 + 
 +   ​-- ​ Move the cursor to the first position of the Quotes vector.
 +   ​Q_Cursor := Quotes.First;​
 + 
 +   ​-- ​ Insert an element at the first position of the vector.
 +   ​Quotes.Insert (Before ​  => Q_Cursor,
 +                  New_Item => To_Unbounded_String ("New index 0!"));
 + 
 +   ​-- ​ Move the cursor to the 5th. position of the vector
 +   ​Q_Cursor := Quotes.To_Cursor (Index => 4);
 + 
 +   ​-- ​ Insert an element before the current 5th. position in the vector and
 +   ​-- ​ set the current position of the cursor.
 +   ​Quotes.Insert (Before ​  => Q_Cursor,
 +                  New_Item => To_Unbounded_String ("New index 4!"),
 +                  Position => Q_Cursor);
 + 
 +   ​-- ​ Move the cursor to the next position
 +   ​Q_Cursor := Next (Position => Q_Cursor);
 + 
 +   ​-- ​ Insert two new elements before the current 6th. position in the vector.
 +   ​Quotes.Insert (Before ​  => Q_Cursor,
 +                  New_Item => To_Unbounded_String ("New index 5 and 6!"),
 +                  Count    => 2);
 + 
 +   ​-- ​ Move the cursor to the 10th. position of the vector.
 +   ​Q_Cursor := Quotes.To_Cursor (Index => 9);
 + 
 +   ​-- ​ Insert two elements before the 10th. position and set the current ​
 +   ​-- ​ position of the cursor.
 +   ​Quotes.Insert (Before ​  => Q_Cursor,
 +                  New_Item => To_Unbounded_String ("New index 9 and 10!"),
 +                  Position => Q_Cursor,
 +                  Count    => 2);
 + 
 +   ​-- ​ Move the cursor to the last position of the vector.
 +   ​Q_Cursor := Quotes.Last;​
 + 
 +   ​-- ​ Insert two empty elements before the last position and set the position
 +   ​-- ​ of the cursor.
 +   ​Quotes.Insert (Before ​  => Q_Cursor,
 +                  Position => Q_Cursor,
 +                  Count    => 2);
 + 
 +   ​-- ​ Move the cursor to the 1st. postion of the vector.
 +   ​Q_Cursor := Quotes.First;​
 +   for i in Quotes.First_Index .. Quotes.Last_Index loop
 +      --  Output the element of the current position.
 +      SUIO.Put_Line (Item => To_Index (Position => Q_Cursor)'​Img & 
 +                     ​Element (Position => Q_Cursor));
 + 
 +      --  Move the cursor to the next position in the vector.
 +      Next (Position => Q_Cursor);
 +   end loop;
 +end Quotes;
 +</​code>​
 +
 +[[Ada.Containers.Vectors#​Vectors.Insert | Go back]]
  

Navigation