473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3703
"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 about time that all of the file and directory stuff...
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. Now I wish to add some attributes for each of the...
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) : m_file_size(file_size),
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 std::vector<T> (or similar). T is non trivial (i.e. not...
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 and since I need to sort these containers quite...
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 ObjectDataSource and would like to have this support...
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. int, double etc. (something along the lines of...
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 example I have is a unit object. The object controls...
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 tv shows and their networks) one line at a time...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.