Container Question

L

Lee Garrington

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?

How do I tell it that 2 points are equal only when both point.x and point.y
are equal?

Thx in advance

Lee
 
D

Donovan Rebbechi

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 said:
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,
 
K

Kevin Saff

Lee Garrington said:
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?

bool operator < (point const& lhs, point const& rhs)
{
if (lhs.x != rhs.x)
return lhs.x < rhs.x;
else
return lhs.y < rhs.y;
}
How do I tell it that 2 points are equal only when both point.x and point.y
are equal?

bool operator == (point const& lhs, point const& rhs)
{
return (lhs.x == rhs.x) && (lhs.y == rhs.y);
}
Thx in advance

Lee

HTH
 
R

red floyd

Lee said:
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?

How do I tell it that 2 points are equal only when both point.x and point.y
are equal?

Thx in advance

Lee

write

bool operator<(const POINT&, const POINT&)

with the properties that you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top