Firstly, is the iterator an actual number, e.g. does v.begin give a 0, namely the first index of vector v? |
No. An iterator can be thought of as analogous to a pointer. Conceptually, an iterator is more general than an index, since it can be used to iterate sequences that can't be indexed, such as a linked list.
Secondly, why do I need the distance function? Can't I simply cout<<result ? |
Iterators don't normally have operator<<() overloads, so they can't be printed, but even if you could print them, all you'd get would be a memory address. Like a pointer, an iterator by itself doesn't know how far from the start of the structure it's pointing.
You can dereference the iterator (
*result
) but this will just give you the element at that position.
You can also subtract instead of calling std::distance() (
v.begin() - result
), but for a vector iterator this is equivalent to std::distance().