// C++03 solution won't work with arrays and some other custom containers. // Mostly drop this block:
something like the above is a viable approach. I wrote a bunch of this in C ++ 11, but included comments on how to cut out excess C ++ 11 stuff (in some cases this simplifies the code, but removes the ability to handle some container-like stuff).
The main idea is to sort the vector of iterator in the source container. Then we create a temporary container, add trivial value_type , swap those trivial value_type with the correct data from the original container (as determined by vector sorted iterator s), then swap this temporary container for our original container.
There are many suggestions, but hopefully from cheap things.
For this to work, the data you sort must be trivial. For this to be effective, the data you work with when trivially constructed must be cheap and swap must be efficient.
I tried to make it as ADL friendly as I can, because I think this is good practice.
Yakk
source share