# Thread: Sorting Vector of structs.

1. Originally Posted by Zosden
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!

2. 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. Originally Posted by Zosden
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.

4. That still doesn't fix my original problem.

5. Originally Posted by Zosden
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.

6. what would I pass a new Comparator

7. 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. Originally Posted by Zosden
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() );

9. 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. Originally Posted by Zosden
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. 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. 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.

Popular pages Recent additions