Use of iterators over array indices

前端 未结 9 1455
执念已碎
执念已碎 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:27

    I presume you are talking about when using a vector, right?

    The main advantage is that iterator code works for all stl containers, while the array indexing operator [] is only available for vectors and deques. This means you are free to change the underlying container if you need to without having to recode every loop. It also means you can put your iteration code in a template and it will work for any container, not just for deques and vectors (and arrays of course).

提交回复
热议问题