Reputation: 109
I would like to search for a specific number and have an iterator track the array element being tested. If the number is found I would like to return the index of the array where the number is found. Here's what I have so far:
vector<int>::iterator iterator;
find(vector.begin(),vector.end(), num);
if(//the number is found in the search)
{
return //where the number is found
}
I'm not quite sure how to sync the iterator up with the element of the array being tested. I would appreciate any help toward solving this problem if possible.
Upvotes: 0
Views: 285
Reputation: 3527
This should do what you want:
std::vector<int> v = {500, 600, 700};
auto it = std::find(v.begin(),v.end(), 600);
std::size_t pos = it - v.begin(); //found(iterator) - begin(iterator)
// of course first you should check if (it != v.end())
Upvotes: 2