vector<int>::iterator it = find(list_vector.begin(), list_vector.end(), 5)
std::find
searches in the range defined by its first two arguments. It returns an iterator pointing to the first element that matches. If no element matches, it returns its 2nd parameter.
list_vector.begin()
returns an iterator that points to the first element of list_vector
.
list_vector.end()
returns an iterator that points one element beyond the final element of list_vector
.
5
is the target of the search. find()
will look for an element that has the value 5
.
If you'd like to determine if 10 is present anywhere in the vector, do this:
if(std::find(list_vector.begin(), list_vector.end(), 10) == list_vector.end())
std::cout << "No 10, bummer\n";
else
std::cout << "I found a 10!\n";
Or, if you'd like to simultaneously determine if 10 is present and determine its location:
std::vector<int>::iterator it = std::find(list_vector.begin(), list_vector.end(), 10);
if(it == list_vector.end())
std::cout << "No 10\n";
else
std::cout << "Look what I found: " << *it << "\n";