Sorting Vector of structs.

This is a discussion on Sorting Vector of structs. within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Zosden They aren't wrong and I know how to use them, and they do point to something. ...

  1. #31
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Quote Originally Posted by Zosden View Post
    They aren't wrong and I know how to use them, and they do point to something. I'm not an idiot. I just wanted to know how to sort the vector in c++. Could you show me an example. you don't have to use my code. I'm more than capable of making them pointers from an example some shows me.
    Actually, I beg to differ. Learning to do something completely wrong is probably worse than not learning it at all. Continuing along that path would make you an idiot in most people's books. It's also entirely likely that all the problems your having are because of how you've wrongly misused pointers.

    Can you even show that everything is allocated correctly, and that everything is deleted correctly afterwards, because I seriously doubt it!

    So, no I wont help you shoot yourself in your foot, or give you enough rope to hang yourself!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #32
    Registered User
    Join Date
    May 2008
    Posts
    30
    Yes I know everything works. I'm not a brand new to programming, and I've tested my code thoroughly. I just wanted to know how to sort a vector of pointers. The assignment is to have a vector of pointers. I just made everything a pointer to learn how to use them. for hopefully the last time, I know I made it harder than I needed to I just wanted to learn how to use them.

  3. #33
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Zosden View Post
    Yes I know everything works. I'm not a brand new to programming, and I've tested my code thoroughly. I just wanted to know how to sort a vector of pointers. The assignment is to have a vector of pointers. I just made everything a pointer to learn how to use them. for hopefully the last time, I know I made it harder than I needed to I just wanted to learn how to use them.
    Then get rid of all the other pointers and just use a vector of pointers

    vector<int*> instead of vector<int*>*
    vector<Election::Candidate*> instead of vector<Election::Candidate*>*

    Then you'll still be using pointers, but you'll be using them in a more sane way.
    "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

  4. #34
    Registered User
    Join Date
    May 2008
    Posts
    30
    That still doesn't fix my original problem.

  5. #35
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Zosden View Post
    That still doesn't fix my original problem.
    What happens when you pass your Comparator class to std::sort()?

    BTW, I wouldn't bother passing the pointers by reference:
    Code:
    bool operator () (const View::Election::Candidate*& lhs , const View::Election::Candidate*& rhs){
    Since you're not planning on modifying the actual pointers themselves and you won't gain any performance benefit on a 4 or 8 byte pointer. They'll just cause more confusion than anything else.
    Last edited by cpjust; 10-20-2008 at 03:21 PM.
    "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

  6. #36
    Registered User
    Join Date
    May 2008
    Posts
    30
    what would I pass a new Comparator

  7. #37
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So that comparator looks right to me, without testing it. You would just give a Comparator object as the third parameter to std::sort.
    Code:
    Comparator who_wins;
    std::sort(whatever.begin(), whatever.end(), who_wins);

  8. #38
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Zosden View Post
    what would I pass a new Comparator
    I believe you'd want to do something like this:
    Code:
    std::sort( myElections->at(i)->myCandidates->begin(),
               myElections->at(i)->myCandidates->end(),
               Comparator() );
    "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

  9. #39
    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
    These lines are for the line bool operator is on
    error C2653: 'View' : is not a class or namespace name
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2143: syntax error : missing ',' before '*'

    These lines are for the next line
    error C2065: 'lhs' : undeclared identifier
    error C2227: left of '->myTotalNumberOfVotes' must point to class/struct/union/generic type

  10. #40
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Zosden View Post
    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
    Ditch the invalid dereferencing.

  11. #41
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. Well, where did View come from? You never had it before.
    2. The parentheses in my example were not superfluous; right now you are dereferencing lhs, but you need to dereference lhs->myTotalNumberofVotes.

  12. #42
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    You stated earlier that your goal in all this was to learn to use pointers. Then for heaven's sake, learn to use pointers for something that they were actually meant to be used for.
    If you were given a screw driver to learn how to use, you wouldn't learn how to use it by trying to hammer nails in with it.

    If you want to learn how to use pointers, then try writing your own linked list or doubly-linked list. That's a good way to learn all about pointer manipulation.
    If you goal is instead actually to write good code, as you would if you were employed as a developer, then you need to drop the unnecessary pointers from the code you're using.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 3 of 3 FirstFirst 123
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, 01: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, 02:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21