Set and Maps in STL
Hello,
I'm experiencing a strange problem with STL's set and maps (probably I'm
misusing the container).
When I instantiate a type like this:
std::set<CMyclass> myset;
I'm required to define an operator < for CMyclass in order for the set
(and map) to work.
However I'm wondering why I am not forced to define also an operator ==
(if I do, it is ignored) since to check whether a certain object is in
the set I need to test for exact match and not only for a greater/lesser
relation.
For example:
CMyclass A;
// fill A with proper data
// ...
// check whether the same object is already in myset
if(myset.find( A ) == myset.end()) {
// object not present, insert it
myset.insert( A )
// process object since it's the first time I see it
// ...
} else {
// object has already been seen, just ignore it!
// ...
}
My class for example can hold the coordinates of a pixel
class CMyclass {
public:
double x,y;
bool operator<(const CMyclass &P) const
{
if(x != P.x) return x < P.x;
return y < P.y;
}
};
Regards,
Giuliano Bertoletti