Sorting Objects in Vector

Yes, I realize it wasn't the best idea to use a vector for this...

Code: http://pastebin.com/LgqRXSe3

The vector should sort the employees with the largest pay per hour to the ones with the smallest pay per hour. What's the best way to do this? Just point me in the right direction, don't give me any code.

Thanks guys. :)

Another Question: I overloaded the extraction operator to enter the employees hourly wages, but the value of 'pph' isn't changing when I assign it a new value. What's going on there? Shouldn't the value held in 'pph' change?

1
2
3
std::istream& operator >> (std::istream &is, const Employee e1) {
    return is >> e1.pph;
}


And another: Is there anyway I could improve upon this program? Other than splitting it into separate files (I feel no need to do that on such a small project). I was looking at for_each and thought I might implement that, but I'm not sure.
Last edited on
You can use the Standard Template Library sort algorithm.

http://www.cplusplus.com/reference/algorithm/sort/


I overloaded the extraction operator

Insertion operator


That should work but I am too tired to run it myself. Good luck.

Last edited on
Insertion operator


Are you sure? The tutorial says the insertion operator is <<.
http://www.cplusplus.com/doc/tutorial/basic_io/

You can use the Standard Template Library sort algorithm.

http://www.cplusplus.com/reference/algorithm/sort/


Okay, checking it out. :)
Last edited on
I overloaded the extraction operator to enter the employees hourly wages, but the value of 'pph' isn't changing when I assign it a new value. What's going on there? Shouldn't the value held in 'pph' change?


Why is e1 passed as a const value instead of as a non-const reference?

Is there anyway I could improve upon this program? Other than splitting it into separate files (I feel no need to do that on such a small project). I was looking at for_each and thought I might implement that, but I'm not sure.
std::for_each() is already available in the algorithms library.
Otherwise, if you're using GCC 4.6+, C++11's range-based for() loops are available to you.

Anyway. I see you like passing vectors of Employees by value to your functions. Passing by value, instead of const reference means copies will be created, whereas you don't need them... which means wasted memory and wasted time.

Check out IceThatJaw's suggestion of using std::sort().
Why is e1 passed as a const value instead of as a non-const reference?


Meh, this is why I shouldn't be working this late at night.

Anyway. I see you like passing vectors of Employees by value to your functions. Passing by value, instead of const reference means copies will be created, whereas you don't need them... which means wasted memory and wasted time.


It works, but I get a warning saying it discards the const. Any reason I am getting that?
Can't be sure without seeing the code.
But I'd wager you forget to add the ampersand &?
http://pastebin.com/Ef1Y9VRJ

The same goes for the other function(sortEmployees).

But I'd wager you forget to add the ampersand &?

No. If I did then it wouldn't be passing by reference. Q.Q
Last edited on
It works, but I get a warning saying it discards the const. Any reason I am getting that?

There are two kinds of iterators: iterator (which you use now) and const_iterator. If you do not change values in a vector, you can pass it by const reference then use a constant iterator.

Also, for functions that have no parameters you use void as in int func(void);.
This is how you do things in C. But in C++, int func(); is the way to go.
There are two kinds of iterators: iterator (which you use now) and const_iterator. If you do not change values in a vector, you can pass it by const reference then use a constant iterator.

Works like a charm! Thank you so much.

Also, for functions that have no parameters you use void as in int func(void);.
This is how you do things in C. But in C++, int func(); is the way to go.

I know, it's just became a habit coming from C. I'll try to kick it. :)

Are there any books you would recommend that cover what you introduced to me? My last book never even mentioned const_iterators.

Do you have a Skype or something? I'm not going to bombard you with questions, but you seem to know what you are doing so I would rather ask you than ask someone else.
Last edited on
I think you have enough inertia momentum to just use a Reference, instead of reading a book:
http://cplusplus.com/reference/
http://en.cppreference.com/w/

Otherwise...
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html/ (free e-book)
... and Effective C++ and Effective STL by Scott Meyers should be enough.
Maybe look up C++11 to familiarize yourself with the 2011 version of the language.

Anyway, back to the sorting problem. Try using std::sort(), and see what happens.

Edit:
Do you have a Skype or something?

I suggest you stick to the forums. It's just by chance (or timezone) that the heavy weight contributors aren't around.
Last edited on
you seem to know what you are doing

That's been his plan all along. Don't let him fool you.
Last edited on
Topic archived. No new replies allowed.