Sort list using STL sort function
Solution 1
The standard algorithm std::sort
requires random access iterators, which std::list<>::iterator
s are not (list iterators are bidirectional iterators).
You should use the std::list<>::sort
member function.
Solution 2
std::list
has a built-in sort
method that you need to use since std::sort
only works with random access iterators, whereas std::list::iterator
merely belongs to the bidirectional iterator class of iterators.
Result.poly.sort(SortDescending());
Also, your operator ()
should be marked const
.
struct SortDescending
{
bool operator()(const term& t1, const term& t2) const
{
return t2.pow < t1.pow;
}
};
Finally, if the type term
overloads an appropriate operator>
you might not need to write your own comparer for sorting — simply use std::greater<T>
(located in the standard header <functional>
):
Result.poly.sort(std::greater<term>());
Solution 3
It seems like the iterator types for Result.poly
is missing operator -
. std::sort
doesn't work with std::list
change to Result.poly.sort
Related videos on Youtube
Vlad
CS student I enjoy sports, music, video games, parties and of course programming...
Updated on April 21, 2022Comments
-
Vlad about 2 years
I'm trying to sort a list (part of a class) in descending order containing items of a
struct
, but it doesn't compile:error: no match for 'operator-' in '__last - __first'
sort(Result.poly.begin(), Result.poly.end(), SortDescending());
And here's
SortDescending
:struct SortDescending { bool operator()(const term& t1, const term& t2) { return t2.pow < t1.pow; } };
Can anyone tell me what's wrong?
-
Vlad about 14 years@Glen see stackoverflow.com/questions/2425452/…
-
-
Andreas Brinck about 14 yearsNo this isn
t it, there's nothing in the standard that says that this needs to be const. If you look at the error message it seems like
operator -` is missing for the input iterators. -
visitor about 14 yearsIt would make a better answer if it was reordered (const-ness is a side-issue here).
-
Vlad about 14 yearsstill doesn't work either with my own comparer or using greater() it still gives a bunch of errors
-
Vlad about 14 yearsbut i don't know how to overload correctly the less operator for my class
-
Vlad about 14 yearsbut i don't know how to overload correctly the less operator for my class
-
Konrad Rudolph about 14 years@Andreas: my concern was that a temporary object gets passed into the
sort
function. I had forgotten that the comparer is passed by value and since temporaries cannot be bound to non-const
references this would have required the function to beconst
. -
Konrad Rudolph about 14 years@Vlad: what errors? This code should work. Did you include the header
<functional>
forstd::greater
? -
Glen about 14 years@Vlad, you don't need to overload anything.
Result.poly.sort(SortDescending());
should work just fine. -
Andreas Brinck about 14 years@Vlad you can call this by
Result.poly.sort(SortDescending())
, no need foroperator <
. -
Andreas Brinck about 14 years@Konrad I think he was talking about
operator <
and had missed the fact that there's a version ofstd::ist::sort
that takes a predicate. -
Konrad Rudolph about 14 yearsThe
operator ()
in your comparer should still be markedconst
as it doesn’t modify any members. -
UncleBens about 14 years
std::greater
will only work ifoperator>
is overloaded forterm
which probably isn't the case here.