Differences

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

Link to this comparison view

vectors.reverse_find_index [2012/01/24 07:57] (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;​
 +   ​Needle ​   : Unbounded_String;​
 +   ​Pos ​      : Extended_Index;​
 +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 search. This will fail because we have no FooBar quote.
 +   ​Needle := To_Unbounded_String ("​FooBar"​);​
 +   Pos := Quotes.Reverse_Find_Index (Item  => Needle);
 + 
 +   if Pos = No_Index then
 +      IO.Put_Line ("​No_Index"​);​
 +   else
 +      IO.Put_Line (Pos'​Img);​
 +   end if;
 + 
 +   ​-- ​ Second search. This will succeed, finding the quote at index 7.
 +   ​Needle := To_Unbounded_String ​
 +     ​("​Most days I wake up thinking I'm the luckiest bastard alive."​);​
 +   Pos := Quotes.Reverse_Find_Index (Item  => Needle);
 + 
 +   if Pos = No_Index then
 +      IO.Put_Line ("​No_Index"​);​
 +   else
 +      IO.Put_Line (Pos'​Img);​
 +   end if;
 + 
 +   ​-- ​ Third search. This will fail, because we start the search at index 4.
 +   ​Needle := To_Unbounded_String ​
 +     ​("​Most days I wake up thinking I'm the luckiest bastard alive."​);​
 +   Pos := Quotes.Reverse_Find_Index (Item  => Needle,
 +                                     Index => 4);
 + 
 +   if Pos = No_Index then
 +      IO.Put_Line ("​No_Index"​);​
 +   else
 +      IO.Put_Line (Pos'​Img);​
 +   end if;
 +end Quotes;
 +</​code>​
 +
 +[[Ada.Containers.Vectors#​Vectors.Reverse_Find_Index | Go back]]
  

Navigation