Hi all,
struct MyStruct {
int x, y;
};
bool operator<(const MyStruct& a1, const MyStruct& a2)
{
return ( (a1.x < a2.x) ||
( (a1.x == a2.x) && (a2.y < a2.y) ) );
}
typedef set<MyStruct> MySet;
Which other operators have I to overload to manage MySet? More
precisaly, when I insert something in the set, if the x field is equal
to another x field of another structure previous inserted in the set,
the second one is not inserted. I would like that a structure is not
inserted if and only if both its x and y filed are equals to x and y of
another structure in the set.
Thanks in advance,
Giulio 6 1411
Giulio Veronesi wrote: struct MyStruct { int x, y; };
bool operator<(const MyStruct& a1, const MyStruct& a2) { return ( (a1.x < a2.x) || ( (a1.x == a2.x) && (a2.y < a2.y) ) ); }
typedef set<MyStruct> MySet;
Which other operators have I to overload to manage MySet?
None. Assignment is useful, but the default one should suffice.
More precisaly, when I insert something in the set, if the x field is equal to another x field of another structure previous inserted in the set, the second one is not inserted. I would like that a structure is not inserted if and only if both its x and y filed are equals to x and y of another structure in the set.
All that should be expressed in the operator <. In your case, for a set
of your structs, two structures k and m are considered equivalent iff
(!(k < m) && !(m < k)) evaluates to true.
V
Hi
Giulio Veronesi wrote: bool operator<(const MyStruct& a1, const MyStruct& a2) { return ( (a1.x < a2.x) || ( (a1.x == a2.x) && (a2.y < a2.y) ) );
Look carefully for an error in the above line...
}
Markus
Markus Moll wrote: Hi
Giulio Veronesi wrote:
bool operator<(const MyStruct& a1, const MyStruct& a2) { return ( (a1.x < a2.x) || ( (a1.x == a2.x) && (a2.y < a2.y) ) );
Look carefully for an error in the above line...
Nope don't see it.
john
"John Harrison" <jo*************@hotmail.com> wrote in message
news:aY******************@newsfe5-win.ntli.net... bool operator<(const MyStruct& a1, const MyStruct& a2) { return ( (a1.x < a2.x) || ( (a1.x == a2.x) && (a2.y < a2.y) ) );
Look carefully for an error in the above line...
Nope don't see it.
It is amazing how some bugs just don't want to reveal themselves. :) One of
the 2s should be 1...
Ali
John Harrison wrote: ( (a1.x == a2.x) && (a2.y < a2.y) ) );
Look carefully for an error in the above line...
Nope don't see it.
Okay:
"a2.y < a2.y" is always false.
That makes "(a1.x == a2.x) && (a2.y < a2.y)" false.
Therefore, effectively, you return a1.x < a2.x
A simple typo, but it breaks your order...
hth
Markus
Markus Moll wrote: John Harrison wrote:
( (a1.x == a2.x) && (a2.y < a2.y) ) );
Look carefully for an error in the above line...
Nope don't see it.
Okay:
"a2.y < a2.y" is always false. That makes "(a1.x == a2.x) && (a2.y < a2.y)" false. Therefore, effectively, you return a1.x < a2.x
A simple typo, but it breaks your order...
Aargh! I blame those spurious parens.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: greg |
last post by:
Discussion is invited on the following proto-PEP.
-------------------------------------------------------------
PEP ??? - Overloadable Boolean Operators...
|
by: GianGuz |
last post by:
Global new and delete operators can be overloaded to suite particulars
needs. Typically they are overloaded to insert useful debugging/trace
informations. What I would to discuss here concerns the...
|
by: bearophileHUGS |
last post by:
Sometimes I suggest to add things to the language (like adding some set
methods to dicts), but I've seen that I tend to forget the meaning of
six set/frozenset operators:
s & t s &= t
s | t s...
|
by: jas_lx |
last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes
fairly simple, as long as one has a fundamental understanding of bits,
bytes and binary.
Having done some Win32...
|
by: Steve Summit |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
It's often explained that the reason for some of the imprecision
in C's definition is so that C can be implemented on different
kinds of machines -- say, those...
|
by: raju |
last post by:
hi
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s
|
by: Syanide |
last post by:
here's a bit info for you fellas:
eg.. you have Square classes u wanna add
public static int operator+ (Square s1, Square s2)
{
// your code here
}
comparision operators..
|
by: Steve R. Hastings |
last post by:
I have been studying Python recently, and I read a comment on one
web page that said something like "the people using Python for heavy math
really wish they could define their own operators". The...
|
by: dspfun |
last post by:
I'm trying to get a good understanding of how unary operators work and
have some questions about the following test snippets.
int *p;
~!&*++p--;
It doesn't compile, why? The problem seems to be...
|
by: Zach |
last post by:
Can someone list the various macro operators and what they mean. Came
across a function macro:
#define max(a, b) ((a)>(b)?(a):(b))
What does "?" amd ":" mean in this statement?
Zach
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
|
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...
| |