If the function checks the index then a general approach in such a case is to throw an exception std::out_of_range.
Take into account that there is already the member function at in the class template std::vector.
If you may not use exceptions and the task is
P.S. Original task is searching item with property equal to.
then you can use either the standard algorithm std::find_if that returns an iterator or you can write your own function that returns the index that corresponds to the searched element. If there is no such an element then return the size of the vector, For example
#include <iostream>
#include <vector>
template <typename Predicate>
std::vector<int>::size_type find_if( const std::vector<int> &v, Predicate predicate )
{
std::vector<int>::size_type i = 0;
while ( i != v.size() && !predicate( v[i] ) ) ++i;
return i;
}
int main()
{
std::vector<int> items = {1, 2, 5, 0, 7};
auto i = ::find_if( items, []( const auto &item ) { return item % 2 == 0; } );
if ( i != items.size() ) std::cout << items[i] << '\n';
return 0;
}
std::vector::at(), which does exactly what you're trying to do?