Get the indices of a range elements by relative order of the values
Two thing I noticed I can do are:


use std::less&lt;T&gt; as a default comparator class
set a default value for the comparator parameter, using the default initializer of the comparator class


The combination of these two cuts code length by half without reducing legibility (!):

template&lt;
    typename RAIterator, 
    typename Size = size_t, 
    typename Compare = std::less&lt;typename std::iterator_traits&lt;RAIterator&gt;::value_type&gt;
&gt;
inline std::vector&lt;Size&gt;
sorted_indices(RAIterator first, RAIterator last, Compare compare = Compare())
{
    std::vector&lt;Size&gt; result(last - first);
    std::iota(result.begin(), result.end(), (Size) 0);
    auto lookup_and_compare = [&amp;first, compare](Size lhs, Size rhs) {
        return compare(first[lhs], first[rhs]);
    };
    std::sort(result.begin(), result.end(), lookup_and_compare);
    return result;
}

template&lt;
    typename Container,
    typename Size = typename Container::size_type,
    typename Compare = std::less&lt;typename Container::value_type&gt;
&gt;
inline std::vector&lt;Size&gt; sorted_indices(Container&amp; container, Compare compare = Compare())
{
    return sorted_indices&lt;typename Container::const_iterator, Size&gt;(
        std::begin(container), std::end(container), compare);
}


The downside is that you must then depend on &lt;iterator&gt; and &lt;functional&gt;; since I use them already for other things, I don't really mind.