Sorting a List with STL sort Function
When sorting a list in descending order using the STL sort function, you may encounter a compilation error if your list contains items of a struct and the comparator function is not properly defined. The error "no match for 'operator-' in '__last - __first'" indicates that the sort function cannot determine the order of elements in your list.
The issue arises because the sort function requires random access iterators, which std::list
To resolve this issue, you should use the std::list
Here's an example of how you can use the std::list
Result.poly.sort([](const term& t1, const term& t2) { return t2.pow < t1.pow; });
This custom comparator function will properly sort your list of structs in descending order based on the pow field within each struct.
The above is the detailed content of Why am I getting 'no match for 'operator-' in '__last - __first'' when sorting a list in descending order using STL sort?. For more information, please follow other related articles on the PHP Chinese website!