D
Debo
Hello all,
I've got a fairly basic question about STL sets and operator overloading
in general.
I'm working with a data type that's essentially a 2-element vector. A
simplified (hacked) version would be something like this:
struct twotuple
{
...
int x;
int y;
};
Now, what I want to be able to do is to use a set to take the union of a
whole bunch of vectors, so that similar vectors will only be stored once.
For instance:
----------------------
set<twotuple> s;
twotuple t1(1, 2);
twotuple t2(2, 1);
twotuple t3(1, 2);
s.insert(t1);
s.insert(t2);
s.insert(t3);
-----------------------
should result in there only being two vectors stored in s (namely, t1 and
t2). I'm under the impression that a set determines equality based on operator<;
I'm guessing that it does something like
if (!(a < b) && !(b<a))
to see if two user-defined structures are equal. My question is, how
should I overload operator< in this case for two-tuples? It seems obvious,
but you can't just ensure that the x and y of one tuple are both less than
the x and y of another tuple, because that will result in 'fake'
equalities in some cases (eg (1 2) and (2 1) will fulfill
!(a < b) && !(b < a) ).
I'm sorry if the answer to this is dead obvious and I'm just being dense.
Any help would be appreciated.
-Debo
I've got a fairly basic question about STL sets and operator overloading
in general.
I'm working with a data type that's essentially a 2-element vector. A
simplified (hacked) version would be something like this:
struct twotuple
{
...
int x;
int y;
};
Now, what I want to be able to do is to use a set to take the union of a
whole bunch of vectors, so that similar vectors will only be stored once.
For instance:
----------------------
set<twotuple> s;
twotuple t1(1, 2);
twotuple t2(2, 1);
twotuple t3(1, 2);
s.insert(t1);
s.insert(t2);
s.insert(t3);
-----------------------
should result in there only being two vectors stored in s (namely, t1 and
t2). I'm under the impression that a set determines equality based on operator<;
I'm guessing that it does something like
if (!(a < b) && !(b<a))
to see if two user-defined structures are equal. My question is, how
should I overload operator< in this case for two-tuples? It seems obvious,
but you can't just ensure that the x and y of one tuple are both less than
the x and y of another tuple, because that will result in 'fake'
equalities in some cases (eg (1 2) and (2 1) will fulfill
!(a < b) && !(b < a) ).
I'm sorry if the answer to this is dead obvious and I'm just being dense.
Any help would be appreciated.
-Debo