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; I have a data structure that looks like this. Code: struct Election{ struct Candidate{ vector<int*>* myPrecincts; string* myName; int* myTotalNumberOfVotes; ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    30

    Sorting Vector of structs.

    I have a data structure that looks like this.

    Code:
    struct Election{
    		struct Candidate{
    			vector<int*>* myPrecincts;
    			string* myName;
    			int* myTotalNumberOfVotes;
    		};
    		vector<Election::Candidate*>* myCandidates;
    		string* myName;
    		int* myTotalNumberOfVotes;
    	};
    	vector<Election*>* myElections;
    I was wondering how I could sort the vector<Election::Candidate*>* myCandidates by their total votes. How would I go about doing this.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You use std::sort.

    Notice that std::sort requires you to define operator< on your struct Candidate, or maybe struct Candidate *.

    Did you sprinkle asterisks in your code at random, or do you not intend to actually have any vectors in your code, only pointers to vectors?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    30
    It's a project for school I just wanted to get used to pointers. I'm new to c++ learned java last year. I just made everything a pointer so I could work with them. How would I do what you said.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you can't type std::sort into the little box at the top-right of your browser, I'm not sure how much help I can be.

    Have you ever overloaded an operator before?

    Getting used to pointers is a very good idea. However, you should do so on a project where using pointers will not actually prevent you from actually doing anything. (Hint: this is a project where using pointers will actually prevent you from actually doing anything.)

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    30
    This is what I have so far what am I doing wrong.

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

    Code:
    struct Election{
    		struct Candidate{
    			vector<int*>* myPrecincts;
    			string* myName;
    			int* myTotalNumberOfVotes;
    			bool operator () (const Election::Candidate& lhs , const Election::Candidate& rhs){
    				return lhs.myTotalNumberOfVotes < rhs.myTotalNumberOfVotes;
    			}
    
    		};
    		vector<Election::Candidate*>* myCandidates;

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    myCandidates is a vector of pointers-to-Candidate, not Candidates themselves.

    Actually, that's not true; myCandidates is a pointer to a vector of pointers-to-Candidate. One might hope that it actually points to something worthwhile, but I'm not holding my breath.

    Let me just say this: every asterisk you have in that code is wrong. Take them all out.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    30
    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.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you insist on pointers, then
    Code:
    bool operator () (const Election::Candidate*& lhs , const Election::Candidate*& rhs){
    				return *(lhs->myTotalNumberOfVotes) < *(rhs->myTotalNumberOfVotes);
    			}
    Remember, lhs isn't a real candidate, and myTotalNumberofVotes isn't a real number.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    30
    ok thanks I forgot to add the pointers to that. I probably should take a break from the homework. How do I do the sort part. This is what I have.

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

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Zosden View Post
    ok thanks I forgot to add the pointers to that. I probably should take a break from the homework. How do I do the sort part. This is what I have.

    Code:
    std::sort(myElections->at(i)->myCandidates->begin(), myElections->at(i)->myCandidates->end(), Election::Candidate::operator ());
    The operator() is more-or-less implied, so you would need to pass in something of type Election::Candidate.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    30
    I think that did the trick. Thanks man. Sorry for being short with you I'm just used to people who say something like you did in the beginning and then offer no help cause I'm not doing it their way. I'll post if I have any more questions.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I won't say that I think you've made very good choices, but I have no problem helping you be a fool if that's what you really want. Good luck.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    30
    This is for a class, in which I'm suppose to learn c++. I'm trying to do that I understand nobody would use pointers like this. I used them like this in order to get use to using pointers. By the way I don't think it is sorting the vector. Here's the line that I have that's suppose to sort it.

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

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you don't use a third parameter, it will assume operator<, which you didn't define. You would need something like
    Code:
    std::sort(myElections->at(i)->myCandidates->begin(), myElections->at(i)->myCandidates->end(), /*some object of type Election::Candidate goes here */);
    If you have something of that type handy, put it in, otherwise you'll have to do something scary like
    Code:
    *(*(myElections->at(i)->myCandidates->begin()))
    One * to dereference the iterator back to a Candidate *, since that's what the vector holds, and one * to dereference that into an actual Candidate.

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    30
    Where would I put that in. It won't compile when I do this.

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

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 01:34 PM
  2. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 04: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-09-2002, 12:52 AM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM

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