The sorting procedure is one of the most usable routine in programming.
And all sorting template functions have the same signature:
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
with conditions: the type of dereferenced RandomIt must meet the requirements
of MoveAssignable and MoveConstructible.
It is clear that in the sorting process a lot of records are moving in the memory and
when these records have a big size, these movements, together with calling constructors, can be very expensive.
Solution.
The proposed template function sorting_view is free of that problem - the sorting procedure
is working (and moving) on tiny objects - iterators. As result of that procedure,
the initial array of records is remaining intact; however provided std::vector<int> view
allows indirect access to the sorted array. On many occasions user does not need the sorted
array at all and he/she has a luxury to iterate through sorted and unsorted arrays simultaneously.
In case of real needs of sorted array, it can be obtained via other template function
apply_view with exactly 2*N movements of the records, where N is the size of initial array.
For getting sorted view of arrays of user's defined records the appropriate
Compare template function should be supplied.
Happy sorting,
Boris Kats.