# Thread: Stl Set and struct comparison

1. ## Stl Set and struct comparison

I have a struct:

Code:
struct aCoord {
int a,b;
};
and a set of them:

Code:
std::set<aCoord> myCoords;
In one of my functions, I would like to check whether a newly generated aCoord is equivalent to one of the elements already in myCoords (meaning check whether firstCoord.a==secondCoord.a && firstCoord.b == secondCoord.b).

I have an iterator and am doing the following:

Code:
iterator = myCoords.find(newCoord);
if(iterator == myCoords.end()) {
myCoords.insert(newCoord);
}
This doesn't work because I need to overload the comparison function used by find, but despite the research I've done so far, I can't figure out the proper form of the operator override. Can anyone help me out? Thanks.

2. I may be wrong, but I think that find() simply uses operator< to determine equivalence (if a is not < b and b is not < a, a == b). Try defining operator< for aCoord.

3. That's the part I can't figure out how to write. What is the form for that? Thanks

4. It depends on whether you write it as a member function or not. As a member function:
Code:
bool operator<(const aCoord&) const;
As a regular function:
Code:
bool operator<(const aCoord&, const aCoord&);

5. Originally Posted by pollypocket4eva
That's the part I can't figure out how to write. What is the form for that? Thanks
Note that a set already checks for uniqueness for you - you don't have to do this manually.

6. Originally Posted by EVOEx
Note that a set already checks for uniqueness for you - you don't have to do this manually.
Good catch. I keep forgetting that insert() will gracefully handle duplicates. You can check the return value of insert() to find out if a new element was actually added to the set.

7. I'm not sure I understand what you're doing with the member and regular functions.
Where would I declare those? And how do I check the components (firstCoord.a==secondCoord.a && firstCoord.b == secondCoord.b) or do I not have to do that explicitly?

Also, EVOEx, that's very helpful to know, but in this case I want to explicitly know whether the set already contains a particular item.

8. Oh, so will insert() return false if the element was not added?

9. The insert() approach is not working for me, I get the error: No match for 'operator<'

10. Yes, that's because you must define some sort of comparison operation for the elements of a set. You may want to read the operator overloading tutorial.

11. I've read that, but can't figure out how to apply it here.
Would it be something like this?
Code:
struct aCoord {
int a,b;
bool operator==(const aCoord &other) const
{ return (a == other.a)&&(b == other.b); }

};

12. Anyone?

13. Something like that, but not that. You need operator<, not operator==. sets determine equivalence (operator==) from comparison.

14. Ok, but how can the < operator address my struct? I need to test if first.a==second.a && first.b==second.b
The following does not do that:

Code:
bool operator<(const aCoord &other) const
{ return (a < other.a)&&(b < other.b); }
I can't think of how to structure it.
Thanks very much for your help.

15. Originally Posted by pianorain
I think that find() simply uses operator< to determine equivalence (if a is not < b and b is not < a, a == b).
Originally Posted by pianorain
sets determine equivalence (operator==) from comparison.
I'll put it another way. Take any two numbers (A and B). They can be the same number, or they can be different. Now, either:
1. A < B
2. B < A
3. !(A < B) && !(B < A)
Note that case 3 is the same as A == B. Do you see how every case, including operator==, uses operator<? You just have to make sure that operator< orders elements correctly.

In cases like this, you usually want to use a lexicographical order. In other words, first sort by A, and then sort by B.