473,508 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Container Question

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
Jul 22 '05 #1
3 1175
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?

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.
Jul 22 '05 #2

"Lee Garrington" <ze******@zen.co.uk> wrote in message
news:40*********************@lovejoy.zen.co.uk...
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
--
KCS
Jul 22 '05 #3
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/
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3484
by: kazio | last post by:
Hello, So, I need to have double linked, circular list (last element point to the first one and first one points to the last one). I thought maybe I could use list container from STL, but...
1
2203
by: Wolfgang Lipp | last post by:
my question is: do we need container elements for repeating elements in data-centric xml documents? or is it for some reason very advisable to introduce containers in xml documents even where not...
4
13026
by: Ulrich Sprick | last post by:
Hi all, (DB2 V7.1 for WinNT) I am looking for a way to determine the free space in my tablespace (containers), but I can't find out. The tablespace in question is a system managed tablespace in...
3
2616
by: jignesh shah | last post by:
Hi all, Is there a way to recover a single container if its been corrupted or mark bad without restoring whole tablespace? environment: db28.1/aix5.1/tsm/rs-6000. Regards Jignesh
1
3356
by: Don Hames | last post by:
I have a windows application that has a split container in the client area. In the left panel, I added controls via the designer in VS 2005. In the right panel, I want to dynamically create and...
7
19996
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
2
1968
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
18
1797
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
36
1998
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
3
1500
by: Rob McDonald | last post by:
I am interested in having a container which has properties of both the STL's list and vector. (I want my cake and to eat it too). In my application, I will need to add/remove items from arbitrary...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7333
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
7502
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...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.