I’m rewriting a free function which takes a reference to a std::vector. Based on a given criteria, it then returns the index of the item in the vector or -1 if its not found. I would prefer if it returned a pointer to the item or a std::vector<>::iterator?
What should I consider when deciding what to return?
Return an iterator, and
yourVector.end()if the element is not found.This is what the standard library uses.
Example:
Better is if you can do
because it will work for any container (including arrays):