e: Unfortunately, this solution only works if you had a real bit_vector container (not std::vector<bool>) that specialized the various <algorithm> functions like a std::find().
template <typename ForwardIterator, typename T>
auto find_nth(ForwardIterator first, ForwardIterator last,
std::size_t nth_index, const T& val)
{
first = std::find(first, last, val);
while (nth_index-- && first != last)
{
first = std::find(std::next(first), last, val);
}
return first;
}
template <typename RandomAccessIterator>
auto real_index(RandomAccessIterator first, RandomAccessIterator last,
const bit_vector& sorted, std::size_t n)
{
auto nth_curr = find_nth(sorted.cbegin(), sorted.cend(), n, false);
return std::next(first, std::min(std::distance(sorted.cbegin(), nth_curr),
std::distance(first, last)));
}
get_destination() could be written using two std::count_if()'s, counting over the ranges (first, start], and (start+1, last] that processed 64 bits at a time rather than 1 bit at a time.