Differences

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

Link to this comparison view

vectors.to_index [2012/01/24 08:04] (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;​
 +   ​A_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);
 +
 +   ​-- ​ Point the cursor to the first index and output the index value
 +   ​A_Cursor := Quotes.To_Cursor (Index => Quotes.First_Index);​
 +   ​IO.Put_Line (Item => To_Index (Position => A_Cursor)'​Img);​
 +
 +   ​-- ​ Point the cursor to the 5th. index and output the index value
 +   ​A_Cursor := Quotes.To_Cursor (Index => 5);
 +   ​IO.Put_Line (Item => To_Index (Position => A_Cursor)'​Img);​
 +
 +   ​-- ​ Point the cursor to a non-existant index, convert to an index and 
 +   ​-- ​ check for No_Index
 +   ​A_Cursor := Quotes.To_Cursor (Index => 42);
 +   if To_Index (Position => A_Cursor) = No_Index then
 +      IO.Put_Line (Item => "​No_Index"​);​
 +   end if;
 +end Quotes;
 +</​code>​
 +
 +[[Ada.Containers.Vectors#​Vectors.To_Index | Go back]]
  

Navigation