-
Thanks. I see your point that every case is covered. But I guess I'm having a hard time wrapping my mind around how the operator is applied by find() and so the ordering has me confused. Sorry and thanks for your patience.
If the operator is evaluating <, then I want it to return true only when !(a<b)&&!(b<a) ?
You see where I'm confused?
-
Yes, I see where you're confused; I'm just failing to find the words to explain it well.
find() does what you want it to do; it returns a dereferencable iterator when it finds the element you specified, or else it returns the end iterator. find() uses operator< to do this by only returning a dereferencable iterator when !(a<b)&&!(b<a). You don't have to worry telling find() to return when !(a<b)&&!(b<a); it's already programmed to do that. The important part is providing an operator< for the set to use.
-
So just to flesh it out (and don't worry, I'm not asking you to do my homework, I just want to understand the structure better and I'm not otherwise getting it:
I have:
Code:
struct aCoord {
int a,b;
bool operator<(const aCoord &other) const
{ return // SOME LOGIC HERE; }
};
and:
Code:
iterator = myCoords.find(newCoord);
if(iterator == myCoords.end()) {
myCoords.insert(newCoord);
}
What is the proper logic for the above (if the whole thing is set up correctly in the first place)?
Thanks again, I appreciate it.
-
Your first block of code is correct. Read above where I mentioned lexicographical ordering to get an idea of the kind of logic you need in there.
Your second block of code is fine, but remember that you don't really need to find() before you insert(). insert() will return a value indicating whether or not it actually added a new element to the set.