(@jlb)
Then perhaps you need to study up on operator overloading, the less than operator< to be specific. |
I have indeed studied for classes, I'll however need to have a look into it for structures. Does it mean, though, that for each structure object in the vector, the sort function will compare the age of the structure object that's currently being looked at and the age of the following one, and change their place if they're not in the right order.
In other words, what I don't get is that it sounds like a bubble sort without a nested for loop.
(@fiji885)
Are you not allowed to build your own sort? Here's an example of what I have. |
Yes I could of course build my own sort, just like yours, which would work totally fine, however I'd like to one understand better how I can utilise the sort algorithm from STL and two I'm planning on using this in a game, so the shorter my functions the better. Thank you for the suggestion though!
(@jonnin)
yes, you can build your own sort. Of course. Do you want to do that instead |
Not really, I'd rather code something short and precise which still does the job fine!
(@Thomas1965)
Here are 2 ways to sort your vector - one uses a function object(functor) the other one a function. |
Thank you, this is actually the sort of explanation/illustration I was looking for, It makes more sense now. One question remains though: with the use of the functor
sort(people.begin(), people.end(), PersonSorter())
wouldn't you need to pass argument to
PersonSorter()
? Or is the sort function automatically doing it by itself?
(@keskiverto)
Yes, if you define an operator< that takes two Person objects as arguments. |
What would be your interest in overloading the < operator if it's only to perform a comparison inside the comp function which only purpose is to return a bool? How would the default < operator not perform the same comparison task?