Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sorting with STL containers containing objects of different subclasses

Reply
Thread Tools

sorting with STL containers containing objects of different subclasses

 
 
Koen
Guest
Posts: n/a
 
      06-30-2003
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: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


 
Reply With Quote
 
 
 
 
Koen
Guest
Posts: n/a
 
      06-30-2003
"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


 
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
Sorting an array list containing objects? Rob Meade ASP .Net 4 11-04-2012 04:29 AM
nested stl containers - aliasing stl objects without invokingoperator= Andrey Vul C++ 6 10-22-2009 09:00 AM
Containers and sorting objects vs. pointers Matthias Kaeppler C++ 18 07-17-2005 12:46 PM
Objects containing a smart ptr (pimpl) and stored in STL containers - help please JackC C++ 3 08-13-2004 11:17 AM
unique objects and stl containers Claudio Jolowicz C++ 11 04-10-2004 03:00 AM



Advertisments