I have a question about sorting a deque with class objects. So I have one deque called "rq" which holds, multiple types of data. I want to sort the deque by comparing the TAU values. However, I keep getting insane compiler errors when trying to sort the deque. Below is the the function I tried to do in order to compare TAU values and sort the deque. How would i go about solving this problem?
deque<system>rq
//and so on
struct tauSort
{
bool operator ()( system &a, system &b)
{
return a.getTau() < b.getTau();
}
}
//Blah Blah
system sorting;
sort(rq.begin(), rq.end(), sorting)
This:
system sorting;
should be this:
tauSort sorting;
because you want to pass the sorting functor, not a system
object. And you are forgetting a semicolon at the end of the sort
statement too.
You can also simply do:
sort(rq.begin(), rq.end(), tauSort());
Not to mention that you can pass the system
s by constant reference in your function, since you don't need to modify them. And it's better for you to make the operator()
const as well:
bool operator ()(system const& a, system const& b) const { … }