This simple example fills a vector with numbers 0-9. Then I use find() to find where the 7 is. Then I saw online you can get the index by simply subtracting V.begin() from the iterator that find() returns. Has - been overloaded for iterators to do some magic and return the offset? Or are iterators simply these offsets?
unsigned int num = 10;
vector<unsigned int> V(num);
for(unsigned int i = 0; i < num; i++)
{
V[i] = i;
}
vector<unsigned int>::iterator it = find(V.begin(), V.end(), 7);
if(it == V.end())
{
cout << "Could not find 7 in the vector" << endl;
}
else
{
cout << "Have found 7 in the vector" << endl;
////////// THE LINE IN QUESTION //////////////
cout << "The number 7 is located at index " << it - V.begin() << endl;
}
Thanks,
Dave