Search function

Discussion in 'C++' started by John J, May 2, 2004.

  1. John J

    John J Guest

    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;

    }
    John J, May 2, 2004
    #1
    1. Advertising

  2. John J

    Mike Wahler Guest

    "John J" <...@...> wrote in message
    news:...
    >
    > 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
    Mike Wahler, May 2, 2004
    #2
    1. Advertising

  3. "John J" <...@...> wrote in message
    news:...
    >
    > 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
    John Harrison, May 2, 2004
    #3
  4. John J

    AlesD Guest

    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::eek:stream& target_stream;
    public:
    // constructor
    PrintRaceEntries(const& Race rtlf, std::eek: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
    AlesD, May 3, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TGFrc2htaSBOYXJheWFuYW4uUg==?=

    Google search result like site search!! How?

    =?Utf-8?B?TGFrc2htaSBOYXJheWFuYW4uUg==?=, May 5, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    670
    Lucas Tam
    May 6, 2005
  2. Andy
    Replies:
    1
    Views:
    355
    Jack Klein
    Nov 25, 2003
  3. Anand Pillai

    String search vs regexp search

    Anand Pillai, Oct 12, 2003, in forum: Python
    Replies:
    10
    Views:
    592
    Anand Pillai
    Oct 15, 2003
  4. mason66
    Replies:
    0
    Views:
    421
    mason66
    Jul 27, 2006
  5. Abby Lee
    Replies:
    5
    Views:
    393
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page