Why use iterators instead of array indices?

Take the following two lines of code:

for (int i = 0; i < some_vector.size(); i++)
{
    //do stuff
}

And this:

for (some_iterator = some_vector.begin(); some_iterator != some_vector.end();
    some_iterator++)
{
    //do stuff
}

I’m told that the second way is preferred. Why exactly is this?

27 Answers
27

Leave a Comment