Use of iterators over array indices

前端 未结 9 1445
执念已碎
执念已碎 2020-12-18 22:54

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.

9条回答
  •  独厮守ぢ
    2020-12-18 23:44

    One other slight difference is that you can't use erase() on an element in a vector by index, you must have an iterator. No big deal since you can always do "vect.begin() + index" as your iterator, but there are other considerations. For example, if you do this then you must always check your index against size() and not some variable you assigned that value.

    None of that is really too much worth worrying about but if given the choice I prefer iterator access for the reasons already stated and this one.

提交回复
热议问题