I just wanted to know what is the main advantage of using the iterators over the array indices. I have googled but i am not getting the right answer.
The STL contains algorithms, such as transform
and for_each
that operate on containers. They don't accept indices, but use iterators.
Iterators help hide the container implementation and allow the programmer to focus more on the algorithm. The for_each
function can be applied to anything that supports a forward iterator.