Thread: Sorting Vector of structs.

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It goes where the /*goes here comment*/ was before; you need the begin, you need the end, and then you need something that, if you put () after it compares the things.

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    30
    Would something like this work.

    Code:
    std::sort(myElections->at(i)->myCandidates->begin(), myElections->at(i)->myCandidates->end(), Election::Candidate::operator ());

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Didn't we start here? The third parameter needs to be a function object. I.e., it needs to be something that has an operator(), not an operator() itself. I.e., it needs to be an object of type Election::Candidate, so that when operator () is applied to it, the Election::Candidate:perator() you defined comes out.

    (I mean, maybe operator() will work if it can accept a function pointer in the third spot in place of a function object, but I've never tried.)

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    30
    What would I place in the third parameter exactly or what pointer could I get rid of to make it easier.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If it were me, I would have called my sorting function
    Code:
    bool operator<(const Election::Candidate*& lhs , const Election::Candidate*& rhs)
    made it a free function (i.e., not in a class), made it a friend if necessary, and not used the third parameter to sort at all.

    If for some reason I'm not allowed to overload that operator, I'd make it its own object:
    Code:
    class Bob {
        operator () {const Election::Candidate*& lhs , const Election::Candidate*& rhs) //etc
    }
    and just used Bob for the third parameter.

  6. #21
    Registered User
    Join Date
    May 2008
    Posts
    30
    Where would I overload the operator< at. Outside or inside the class, and If so how does it know to use that operator overload.

  7. #22
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It knows because that's what it will try as a default. If you have a vector container of some type (foo for example) and call the default version of the sort function the compiler will try to use/find the operator< that works with foo objects. If it can't find one then the compiler will complain and you'll get a compiler error.

    As mentioned, you can create a free function version of operator< (not inside the class) that should take two const references to your object type and does a comparison of those two objects returning a true/false boolean value. Then you'd just call the default version of sort and the compiler should take care of things for you at compile time.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #23
    Registered User
    Join Date
    May 2008
    Posts
    30
    The compiler gives me this final error

    Code:
    bool operator<(const View::Election::Candidate*& lhs , const View::Election::Candidate*& rhs){
    	return *lhs->myTotalNumberOfVotes < *rhs->myTotalNumberOfVotes;
    }
    'operator <' must have at least one formal parameter of class type

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I was afraid of that. (Reason 347 not to use pointers -- you can't overload.) Then you'll have to go with curtain 2 and a function object.

  10. #25
    Registered User
    Join Date
    May 2008
    Posts
    30
    which pointer messes it up. I can change it.

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The vector-of that we're trying to sort. Since we're trying to sort pointers, the compiler gives us a frowny face, because pointers are built-in types. If we were trying to sort Election::Candidates themselves, we could define operator<.

  12. #27
    Registered User
    Join Date
    May 2008
    Posts
    30
    Those are the only pointers that I have to have for the assignment. Would having the class that just has the operator< work with the pointers.

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you'll have to go with class Bob and the function object.

  14. #29
    Registered User
    Join Date
    May 2008
    Posts
    30
    Code:
    #ifndef COMPARATOR_H
    #define COMPARATOR_H
    #include "View.h"
    class Comparator{
    	bool operator () (const View::Election::Candidate*& lhs , const View::Election::Candidate*& rhs){
    
    		return *lhs->myTotalNumberOfVotes < *rhs->myTotalNumberOfVotes;
    	}
    };
    #endif
    Is that what I would have to do.

  15. #30
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Does your assignment require you to use a billion pointers? If not, I think your time would be better spent doing it properly without all the pointers, rather than trying to fit a square peg into a round hole...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 03:07 PM
  3. sorting containers of structs
    By bigSteve in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2004, 02:50 PM
  4. sorting an array of structs
    By jo_jo2222 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 11:52 PM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM