I know how to get the index from a vector iterator, by subtracting the begin iterator from it. For example:
vector<int>::iterator it = find(vec.begin(), vec.end(), x);
size_t position = it - vec.begin();
However, now I want to find the index of the last x
in the vector. How can I get the real index from the reverse iterators? I've found the following that seems to work (edit: it doesn't) but maybe there is a better (more idiomatic or whatever..) way.
vector<int>::reverse_iterator it = find(vec.rbegin(), vec.rend(), x);
size_t position = vec.size() - (it - vec.rbegin());