In article <40*********************@lovejoy.zen.co.uk>, Lee Garrington wrote:
Hi,
I want to create a set of POINT's but I need to define the sorting and
equality conditions for that.
How do I tell the set container that I want them ordered ascendingly by
point.x primarily and then by point.y secondarily?
Hint: the template is something like this:
template <class _Key, class _Compare = less<_Key>,
class _Alloc = __STL_DEFAULT_ALLOCATOR(_Key) > class set
So you need to supply a replacement for std::less<_Key> . That is, it should
behave like operator<. Then supply that as the second template argument.
struct mycomp {
bool operator() (const Point& left, const Point& right )
{
// insert code here
}
};
std::set<Point,mycomp> myset;
How do I tell it that 2 points are equal only when both point.x and point.y
are equal?
You're abusing the word "equal". The set container actually doesn't care
whether two elements are "equal", so you don't have to tell it anything like
this.
BTW, there are examples where you can order, but you can't meaningfully test
for equality. For example, you can sort a set of doubles, but floating point
errors make checks for "equality" impossible (unless you have some knowledge
about bounds on the error margins)
Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/