std::distance(result, v.rend()) does not yield the correct index. A minus 1 is missing. The correct index would be std::distance(result, v.rend()) - 1.