开发者

Ada 05 question: Search specific elements in a vector of records?

开发者 https://www.devze.com 2023-03-06 05:54 出处:网络
I\'ve created a Ada program that will be compiled in GNAT Gcc compiler 4.3.0 I created a record that consists of a name, phone, address and a birthday.

I've created a Ada program that will be compiled in GNAT Gcc compiler 4.3.0 I created a record that consists of a name, phone, address and a birthday. The name, phone and address will be in unbounded string format and the birthday another record. The purpose here is to make a addressbook management system.(Very simple one)

type birthday is record
    year  : Positive;
    month : Positive;
    day   : positive;
end record;

type contact_type is record
    name    : unbounded_string;
    phone   : unbounded_string;
    address : unbounded_string;
    bday    : birthday;
end record;

I've already made an insert(using the append) and delete, and list function. I need a search and sort function to finish off.

The thing is I made this record(the contact_type record) into a vector. Now I want to search from the vector for one of the elements/subclass(whatever you call it properly).

For example search for a name... Or an address, etc. But using the vector's find_index method, I need to insert another record of the type contact_type and only an exactly same element will be returned as the result.

But what I want is if I search a name, all the elements with the name is returned...

And will it be possible to "Generic Sort" the vector开发者_如何学运维 in terms of Name, Address, Bday(So i have three different sorting options)

PS. The first Ada program I'm writing is an addressbook manager... I'm a complete newbie, and I only have experience in C, and Java so it's kinda going rough... (It took me a long time finding out how to Standard input and Output lol)


When I started learning Ada, I wrote a rather lengthy Wiki article about Ada.Containers.Vectors. It helped me remember all the many options that are available to you when using this package. In the article there are examples on how to iterate a vector, how to search it and how to sort it.

http://wiki.ada-dk.org/index.php/Ada.Containers.Vectors

Enjoy! :o)


how about simply looping through the content?

function Find_Name (V : Vector; Name : String) return Contact_Type is
   Position : Cursor := V.First;
begin
   while Position /= No_Element loop
      if Element (Position).Name = Name then
         return Element (Position);
      end if;
      Next (Position);
   end loop;
   return Empty_Contact;
end Find_Name;


Instead of Find_Index, use Iterate and pass it a subprogram that examines each visited record.

package Container is new Containers.Vectors (Natural, Contact_Type);
List : Container.Vector;
...
procedure Visit (Position : Container.Cursor) is
    C : Contact_Type := Container.Element(Position);
begin
    -- examine C
end Visit;
...
List.Iterate (Visit'Access);

You can use a similar pattern to construct your sort keys. See also A.18.16 Array Sorting.

Addendum: As @Simon Wright comments, Generic_Sorting in Containers.Vectors is a better choice.


@oenone's answer is fine so long as there's only one matching element. Otherwise, you might try something like

function Find_Name (V : Vector; Name : String) return Vector is
   Position : Cursor := V.First;
   Result : Vector;
begin
   while Position /= No_Element loop
      if Element (Position).Name = Name then
         Append (Result, Element (Position));
      end if;
      Next (Position);
   end loop;
   return Result;
end Find_Name;
0

精彩评论

暂无评论...
验证码 换一张
取 消