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, you don’t need to write your own comparer for that, 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