472,342 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

sorting with STL containers containing objects of different subclasses

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::operator<(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
Jul 19 '05 #1
1 3655
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vg************@corp.supernews.com...
"Koen" <no@ssppaamm.com> 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
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. ...
2
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) :...
3
by: JackC | last post by:
Hi Problem: I wish to use a pimpl to hide implementation/storage of a class (T), but I also want to hold objects of that class (T) in an...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an...
17
by: devmentee | last post by:
Hello, I am trying to create a map/dictionary where the type of key is known ie std::string, but the value could be of any built in type. ie....
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.