Differences

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

Link to this comparison view

vectors.has_element [2012/01/24 08:03] (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);
 + 
 +   ​-- ​ First check. Cursor is not yet pointing at any specific index
 +   if not Has_Element (Position => A_Cursor) then
 +      IO.Put (Item => To_Index (Position => A_Cursor)'​Img & " ");
 +      IO.Put_Line (Item => "No Element!"​);​
 +   end if;
 +   
 +   ​-- ​ Point cursor at the last index
 +   ​A_Cursor := Quotes.To_Cursor (Index => Quotes.Last_Index);​
 +   if Has_Element (Position => A_Cursor) then
 +      IO.Put (Item => To_Index (Position => A_Cursor)'​Img & " ");
 +      IO.Put_Line (Item => "​Element!"​);​
 +   end if;
 +   
 +   ​-- ​ Delete the first position. Now the cursor is pointing at a non-
 +   ​-- ​ existant position, represented by the numeric value -1
 +   ​Quotes.Delete_First;​
 +   if not Has_Element (Position => A_Cursor) then
 +      IO.Put (Item => To_Index (Position => A_Cursor)'​Img & " ");
 +      IO.Put_Line (Item => "No Element!"​);​
 +   end if;
 +   
 +   ​-- ​ Move the cursor on position backwards, from 9 to 8.
 +   ​Previous (Position => A_Cursor);
 +   
 +   ​-- ​ Now check if the current position designates an element
 +   if Has_Element (Position => A_Cursor) then
 +      IO.Put (Item => To_Index (Position => A_Cursor)'​Img & " ");
 +      IO.Put_Line (Item => "​Element!"​);​
 +   end if;
 +end Quotes;
 +</​code>​
 +
 +[[Ada.Containers.Vectors#​Vectors.Has_Element | Go back]]
  

Navigation