search objects in a vector

S

shoa

Hello

I have a vector of client objects. Each client has some fields: name (first
and last names), address, phone number....

I need to have a functions to search clients so that an user can select
fields to search. For example an user can select search first name and phone
number, he can enter value to these fields to search. Other users may want
to search address and last name.... and so on.

It can be seen that I can use if... else clause to search. however this
method is not good if a client has many fields need to search (age,
sex....).

If you have a better solution, please help

Many thanks
S.Hoa
 
B

Bryce

Hello

I have a vector of client objects. Each client has some fields: name (first
and last names), address, phone number....

I need to have a functions to search clients so that an user can select
fields to search. For example an user can select search first name and phone
number, he can enter value to these fields to search. Other users may want
to search address and last name.... and so on.

It can be seen that I can use if... else clause to search. however this
method is not good if a client has many fields need to search (age,
sex....).

If you have a better solution, please help

quick and easy way is to create multiple hash tables/maps that use the
fields to search by as the key, and contain the objects...
 
S

Steve Horsley

shoa said:
Hello

I have a vector of client objects. Each client has some fields: name (first
and last names), address, phone number....

I need to have a functions to search clients so that an user can select
fields to search. For example an user can select search first name and phone
number, he can enter value to these fields to search. Other users may want
to search address and last name.... and so on.

It can be seen that I can use if... else clause to search. however this
method is not good if a client has many fields need to search (age,
sex....).

If you have a better solution, please help

Many thanks
S.Hoa

Create a storage class to store the objects in. The storage class
will contain multiple hash maps, one for each searchable field.

The hash maps will probably have to point to lists rather than
directly to objects, to cater for collisions (e.g. two clients of
the same sex).

Searching for multiple fields could be interesting. One way is to
retrieve the object list for each field and then use the
collections framework to reduce to one list containing the
(hopefully just one) item that is common to all the search
criteria. I'm sure there is a function to do this.

Adding an item will involve adding it to all the hashes. Removing
an item will involve removing it from all the hashes.

Steve
 
J

John English

shoa said:
I have a vector of client objects. Each client has some fields: name (first
and last names), address, phone number....

I need to have a functions to search clients so that an user can select
fields to search. For example an user can select search first name and phone
number, he can enter value to these fields to search. Other users may want
to search address and last name.... and so on.

This is what databases are for, surely... Have a look at Derby (formerly
Cloudscape, from IBM, now an Apache project) which can be embedded into
your application and will just do it all for you (but you;ll need to use
some SQL...).

-----------------------------------------------------------------
John English | mailto:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
Dept. of Computing | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
 
S

shoa

Thank you for all help
I will try hash maps.

The objects storing in the vector are obtained from a mySQL database. I am
not so sure how to use a database to search. I think that I still have to
use if else clauses to select fields to search
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top