C++ - index of element in sorted std::vector

You can use std::lower_bound (O(log(N)) and std::distance (O(1) for random access iterators):

auto lower = std::lower_bound(v.begin(), v.end(), val);
// check that value has been found
const bool found = lower != v.end() && *lower == val;

Then, either

auto idx = std::distance(v.begin(), lower);

or plain arithmetic:

auto idx = lower - v.begin();

You want to use the lower_bound() function. It's a little bit funky to make it generally useful, but serves the purpose you want.