Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Search function

Reply
Thread Tools

Search function

 
 
John J
Guest
Posts: n/a
 
      05-02-2004

I am in the process of writing 3 associated classes to store details of
yacht racing information. Below is a section of code from my Entry class. An
entry object is created as follows "Entry e1 (&r1, &y1,1,"22:30");"
The &r1 is a reference to an associated Race object (Race class) and &y1 is
a reference to an associated Yacht object (Yacht class). The 1 is a place
ie, 1 = first, and "22:30" is the finish time.

I wish to add a function to the Race class that will search all entry
objects (class below) and display all the Yachts that entered a specified
race; however, all my attempts at a search function have failed. I'd
appreciate some advice on this.



#include <iostream>
#include <string>

#include "Entry.h"

using namespace std;

//Construct an Entry

Entry::Entry (Race* r, Yacht* y, int pl, string ti)

{

which = r;

what = y;

place = 0;

time = "";

}

//Access functions

int Entry::getPlace () const

{

return place;

}

string Entry::getTime () const

{

return time;

}

Race* Entry::getRace () const

{

return which;

}

Yacht* Entry::getYacht () const

{

return what;

}

//Overload of operator<< for output of an Entry

ostream& operator<< (ostream& out, const Entry& e)

{

out << "Finish Place: " << e.place << endl

<< "Finish Time: " << e.time << endl

<< "Race Nr (date): " << *(e.which)

<< "Yacht: " << *(e.what) << endl;

return out;

}


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      05-02-2004

"John J" <...@...> wrote in message
news:(E-Mail Removed) s.com...
>
> I am in the process of writing 3 associated classes to store details of
> yacht racing information. Below is a section of code from my Entry class.

An
> entry object is created as follows "Entry e1 (&r1, &y1,1,"22:30");"
> The &r1 is a reference to an associated Race object (Race class) and &y1

is
> a reference to an associated Yacht object (Yacht class). The 1 is a place
> ie, 1 = first, and "22:30" is the finish time.
>
> I wish to add a function to the Race class that will search all entry
> objects (class below) and display all the Yachts that entered a specified
> race; however, all my attempts at a search function have failed. I'd
> appreciate some advice on this.
>
>
>
> #include <iostream>
> #include <string>
>
> #include "Entry.h"
>
> using namespace std;
>
> //Construct an Entry
>
> Entry::Entry (Race* r, Yacht* y, int pl, string ti)
>
> {
>
> which = r;
>
> what = y;
>
> place = 0;
>
> time = "";
>
> }
>
> //Access functions
>
> int Entry::getPlace () const
>
> {
>
> return place;
>
> }
>
> string Entry::getTime () const
>
> {
>
> return time;
>
> }
>
> Race* Entry::getRace () const
>
> {
>
> return which;
>
> }
>
> Yacht* Entry::getYacht () const
>
> {
>
> return what;
>
> }
>
> //Overload of operator<< for output of an Entry
>
> ostream& operator<< (ostream& out, const Entry& e)
>
> {
>
> out << "Finish Place: " << e.place << endl
>
> << "Finish Time: " << e.time << endl
>
> << "Race Nr (date): " << *(e.which)
>
> << "Yacht: " << *(e.what) << endl;
>
> return out;
>
> }


Race r1;
Entry e1 (&r1, &y1,1,"22:30");"

if(*e.getRace == r1)
cout << e << '\n';

-Mike


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-02-2004

"John J" <...@...> wrote in message
news:(E-Mail Removed) s.com...
>
> I am in the process of writing 3 associated classes to store details of
> yacht racing information. Below is a section of code from my Entry class.

An
> entry object is created as follows "Entry e1 (&r1, &y1,1,"22:30");"
> The &r1 is a reference to an associated Race object (Race class) and &y1

is
> a reference to an associated Yacht object (Yacht class). The 1 is a place
> ie, 1 = first, and "22:30" is the finish time.
>
> I wish to add a function to the Race class that will search all entry
> objects (class below) and display all the Yachts that entered a specified
> race; however, all my attempts at a search function have failed. I'd
> appreciate some advice on this.
>


I don't think you've supplied enough information. The definition of the Race
class is essential, as is the relationship between Race and Entry. You've
already said that each Entry has a reference to a Race, presumably each Race
also has references to all the Entries for that race in some manner.

I actually think its questionable that Entry has a reference to Race,
creating circular references (Entry has reference to Race and each Race has
references to all its Entries) is something that shouldn't be done lightly,
in my opinion. Of course it all depends on how everything hangs together in
your final system but I'd be surprised if it is really necessary.

Probably the best thing is not to be shy and to post one of your failed
attempts. Seeing code tends to be much more informative than reading
descriptions of code.

john


 
Reply With Quote
 
AlesD
Guest
Posts: n/a
 
      05-03-2004
John J wrote:
> I am in the process of writing 3 associated classes to store details of
> yacht racing information. Below is a section of code from my Entry class. An
> entry object is created as follows "Entry e1 (&r1, &y1,1,"22:30");"
> The &r1 is a reference to an associated Race object (Race class) and &y1 is
> a reference to an associated Yacht object (Yacht class). The 1 is a place
> ie, 1 = first, and "22:30" is the finish time.
>
> I wish to add a function to the Race class that will search all entry
> objects (class below) and display all the Yachts that entered a specified
> race; however, all my attempts at a search function have failed. I'd
> appreciate some advice on this.
>
>
>
> #include <iostream>
> #include <string>
>
> #include "Entry.h"
>
> using namespace std;
>
> //Construct an Entry
>
> Entry::Entry (Race* r, Yacht* y, int pl, string ti)
>
> {
>
> which = r;
>
> what = y;
>
> place = 0;
>
> time = "";
>
> }
>
> //Access functions
>
> int Entry::getPlace () const
>
> {
>
> return place;
>
> }
>
> string Entry::getTime () const
>
> {
>
> return time;
>
> }
>
> Race* Entry::getRace () const
>
> {
>
> return which;
>
> }
>
> Yacht* Entry::getYacht () const
>
> {
>
> return what;
>
> }
>
> //Overload of operator<< for output of an Entry
>
> ostream& operator<< (ostream& out, const Entry& e)
>
> {
>
> out << "Finish Place: " << e.place << endl
>
> << "Finish Time: " << e.time << endl
>
> << "Race Nr (date): " << *(e.which)
>
> << "Yacht: " << *(e.what) << endl;
>
> return out;
>
> }
>
>


Hello,

first of all you didn't tell us where do you store all entries which
is essential. I'll suppose that you use some kind of container - say list.

std::list<Entry> entry_list;

Then you have to create function or class which will be able to compare
some race with some entry and print the entry if it belongs to the race.
I suggest using class which is able to hold the information as well.
It's definition might look like this:

class PrintRaceEntries
{
private:
const Race& race_to_look_for;
std:stream& target_stream;
public:
// constructor
PrintRaceEntries(const& Race rtlf, std:stream& ts)
: race_to_look_for(rtlf), target_stream(ts) {};

// this function prints it's parameter it's getRace() equals to Race
// supplied in constructor of this class
void operator(const Entry& entry_in_question) {
if (entry_in_question.getRace() == race_to_look_for)
target_stream << entry_in_question;
};
};


Following line will apply operator() of temporary created instance of
PrintRaceEntries to all items in entry_list.

std::for_each(entry_list.begin(), entry_list.end(),
PrintRaceEntries(some_race, std::cout));

Easy, isn't is!?

Of course you can create many functors to implement different behaviour.
There are also other usefull algorithms which might come handy.

Please refer to STL documentation.

Ales

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
removing search engines from the search bar whatever.or.not@gmail.com Firefox 1 02-17-2005 12:06 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Search Bar not displaying search Engines Zimran Douglas Firefox 1 01-07-2005 02:30 PM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM



Advertisments