Presumably, your real code contains all the missing {}
, otherwise there would be far more errors.
T
appears to be the container type, not the value type, so the iterator type would be typename T::iterator
; except that the container is const
, so you can only get a const_iterator
out of it. But your code tries to modify the container, which isn't possible if it's const
. Perhaps you want to copy the argument into a new container, and sort and return that; or perhaps you want to take a non-const reference and sort the container in place (not returning anything).
You're trying to perform random-access operations on an iterator type that doesn't support them. List iterators are bidirectional - you can increment them and decrement them, but you can't add arbitrary amounts to them, or subtract them to give a distance.
Perhaps it would be more appropriate to use a random-access container like vector
. Or perhaps you could replace it+n
with std::next(it,n)
and it2-it1
with std::distance(it1,it2)
. (Or, if you're stuck with a pre-C++11 library, write your own versions of these functions.)
Finally, it2!=mylist.end()-it1
doesn't make sense; the left-hand side is a distance, which can't be compared to an iterator. Perhaps you want end() - (it1-begin())
, referring to the position as far from the end as it1
is from the beginning. More likely, you want to iterate from it1
to end()-1
.