Thread: Stl Set and struct comparison

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    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. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    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. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    That's the part I can't figure out how to write. What is the form for that? Thanks

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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&);
    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

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by pollypocket4eva View Post
    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. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by EVOEx View Post
    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.
    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

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    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. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Oh, so will insert() return false if the element was not added?

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    The insert() approach is not working for me, I get the error: No match for 'operator<'

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    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

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    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. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Anyone?

  13. #13
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Something like that, but not that. You need operator<, not operator==. sets determine equivalence (operator==) from comparison.
    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

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    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. #15
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by pianorain View Post
    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 View Post
    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.
    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