sorting with STL containers containing objects of different subclasses

Discussion in 'C++' started by Koen, Jun 30, 2003.

  1. Koen

    Koen Guest

    Hi!

    I have a vector containing the following:

    vector<Base*> theVector;
    theVector.push_back(new Der1());
    theVector.push_back(new Der1());
    theVector.push_back(new Der2());
    theVector.push_back(new Der2());
    ....

    where the Der1 and Der2 classes are derived from Base (abstract base
    class).
    Base
    +--Der1
    +--Der2

    Each class derived from Base contains a "time stamp" and I have
    implemented the operator: bool Base::eek:perator<(const Base& inEvent)
    const; that compares the time stamp of the object with that of the given
    object.

    Now, I would like to sort theVector according to the time stamp.
    If I just call sort(theVector.begin(),theVector.end()); this doesn't
    work because the "<" operator is never called (in fact, the addresses of
    the objects in the list are compared directly).
    Is there a way to have both things with STL containers:
    1. store different object types (all derived from a single base class)
    in a container (the only way to do this is to store pointers; otherwise
    the objects get "sliced")
    2. be able to use the standard algorithms on these containers

    This must be possible, but I can't seem to find out how right now.
    Any help appreciated!

    Koen
    Koen, Jun 30, 2003
    #1
    1. Advertising

  2. Koen

    Koen Guest

    "Victor Bazarov" <> wrote in message
    news:...
    > "Koen" <> wrote...


    > You need to define the comparator:
    >
    > bool myLessFunc(const Base* p1, const Base* p2)
    > {
    > return *p1 < *p2; // uses your operator<
    > }
    >
    > And pass it to the sort function:
    >
    > sort(theVector.begin(), theVector.end(), myLessFunc);
    >
    > Victor


    Thanks a lot Victor!

    I had in the meantime found something about using functors for these
    things, something like:

    class CompareFunctor
    {
    public:
    bool operator()(const Base* inEvent1,const Base* inEvent2)
    {
    return (inEvent1->GetTimeStamp() < inEvent2->GetTimeStamp()) ? true :
    false;
    }
    };

    But your solution is much simpler and I use that now (although I did add
    the function myLessFunc to my Base class as a static member function,
    for when I would need it at other places too).
    Thanks for the very (really very) fast reply!

    Koen
    Koen, Jun 30, 2003
    #2
    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. Claudio Jolowicz

    unique objects and stl containers

    Claudio Jolowicz, Apr 9, 2004, in forum: C++
    Replies:
    11
    Views:
    2,043
    James Dennett
    Apr 10, 2004
  2. JackC
    Replies:
    3
    Views:
    591
    Alan Griffiths
    Aug 13, 2004
  3. Matthias Kaeppler

    Containers and sorting objects vs. pointers

    Matthias Kaeppler, Jul 15, 2005, in forum: C++
    Replies:
    18
    Views:
    870
    Matthias Kaeppler
    Jul 17, 2005
  4. Replies:
    7
    Views:
    553
    Pete Becker
    Jan 25, 2008
  5. Andrey Vul
    Replies:
    6
    Views:
    569
    James Kanze
    Oct 22, 2009
Loading...

Share This Page