Find k-th smallest index
To find the kth smallest/largest value you do not need to sort all values! Simply keep track of the k smallest/largest encountered values in a (sorted) buffer. This will take your run time from O(nlog(n)) to O(nlog(k)). And will also avoid the unnecessary copy and iteration, simply keep the index with k.

The following pseudocode illustrates the algorithm

template&lt;typename It&gt;
It min_k(It first, It last, int k){
   auto cmp_it_values = [](It lt, It rt){ return *lt &lt; *rt;};
   auto max_copy = std::min&lt;long&gt;(k, std::distance(first, last));
   auto start_it = first;
   std::advance(start_it, max_copy);

    k++; // k == 0 has to return smallest one element.
    std::vector&lt;It&gt; k_smallest;
    k_smallest.reserve(k+1);
    for(auto it = first; it != start_it; ++it){
        k_smallest.push_back(it);
    }
    std::sort(k_smallest.begin(), k_smallest.end(), cmp_it_values);

    for(auto it = start_it; it != last; ++it){
        if(k_smallest.empty() || *it &lt; *k_smallest.back()){
            auto insertion_point = std::lower_bound(k_smallest.begin(), k_smallest.end(), 
                                                    it, cmp_it_values);
            k_smallest.insert(insertion_point, it);
            if(k_smallest.size() &gt; k){
                k_smallest.pop_back(); // Remove the largest value
            }
        }
    }    
    return k_smallest.back(); // The iterator to the min(k, n) smallest value 
}


The above has O(nklog(k)) worst case and O(n + log(k)) best case behaviour. You can improve the worst case by using a max heap for k_smallest instead of a vector. This would have the promised O(nlog(k)) run time. I leave that as an exercise to the reader ;)

And you should read this using namespace std; is bad practice.

You can see from the benchmark here: https://ideone.com/B81Hs4 that the code above is twice as fast as your original code for the given values (you need to test with typical values for your application to verify) and the heap version is even faster.