Differences
This shows you the differences between two versions of the page.
— |
vectors.replace_element [2012/01/23 21:22] (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; | ||
+ | Q_Cursor : Cursor; | ||
+ | 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); | ||
+ | |||
+ | -- Replace the first index | ||
+ | SUIO.Put_Line (Item => Quotes.Element (Index => 0)); | ||
+ | Quotes.Replace_Element (Index => 0, | ||
+ | New_Item => To_Unbounded_String ("New index 0")); | ||
+ | SUIO.Put_Line (Item => Quotes.Element (Index => 0)); | ||
+ | |||
+ | -- Replace the index 7 using a cursor | ||
+ | SUIO.Put_Line (Item => Quotes.Element (Index => 7)); | ||
+ | Q_Cursor := Quotes.To_Cursor (Index => 7); | ||
+ | Quotes.Replace_Element (Position => Q_Cursor, | ||
+ | New_Item => To_Unbounded_String ("New index 7")); | ||
+ | SUIO.Put_Line (Item => Quotes.Element (Index => 7)); | ||
+ | end Quotes; | ||
+ | </code> | ||
+ | |||
+ | [[Ada.Containers.Vectors#Vectors.Replace_Element | Go back]] | ||