# Stl Set and struct comparison

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-13-2010
pollypocket4eva
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.
• 08-13-2010
pianorain
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.
• 08-13-2010
pollypocket4eva
That's the part I can't figure out how to write. What is the form for that? Thanks
• 08-13-2010
pianorain
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&);`
• 08-13-2010
EVOEx
Quote:

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.
• 08-13-2010
pianorain
Quote:

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.
• 08-13-2010
pollypocket4eva
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.
• 08-13-2010
pollypocket4eva
Oh, so will insert() return false if the element was not added?
• 08-13-2010
pollypocket4eva
The insert() approach is not working for me, I get the error: No match for 'operator<'
• 08-13-2010
pianorain
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.
• 08-13-2010
pollypocket4eva
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); }                 };```
• 08-13-2010
pollypocket4eva
Anyone?
• 08-13-2010
pianorain
Something like that, but not that. You need operator<, not operator==. sets determine equivalence (operator==) from comparison.
• 08-13-2010
pollypocket4eva
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.
• 08-13-2010
pianorain
Quote:

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).

Quote:

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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last