Thread: Stl Set and struct comparison

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    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?

  2. #17
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    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.

  4. #19
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mandelbrot set program improvements
    By mad_muppet in forum Game Programming
    Replies: 3
    Last Post: 07-14-2010, 05:58 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM