index of a found string element of a vector using find function

Mar 23, 2022 at 2:05pm
How can I find the position of an element in the vector I am searching the element in for?
Last edited on Mar 23, 2022 at 2:06pm
Mar 23, 2022 at 2:14pm
Perhaps something like:

1
2
3
4
5
6
const auto itr {std::find(vect.begin(), vect.end(), tofind)};

if (itr != vect.end())
    std::cout << "Found at position " << std::distance(vect.begin(), itr) << '\n';
else
    std::cout << "Not found\n";

Topic archived. No new replies allowed.