[Cs3] Question on Code

Mikhail Nesterenko mikhail at cs.kent.edu
Fri May 2 20:54:45 EDT 2014


> What is this line of code doing and why?
> I understand it's the iterator that was returned from the find_if
> What I don't understand is why we must subtract vect.begin() from the
> iterator in order to output it.
> it - vect.begin()
> 

This is iterator arithmetic. The difference is the number of elements
between iterators. Can be applied to random access iterators only. In
this case, we are printing the "position" of the element or its index.

thanks,
--
Mikhail


> 
> auto it = find_if(vect.begin(), vect.end(), passingScore); // find_if
> if (it != vect.end()){
> cout << "Found it at position(s): ";
> do{
> //here-------> cout << it - vect.begin() << " ";
> it = find_if(it + 1, vect.end(), passingScore);
> } while (it != vect.end());


More information about the cs3 mailing list