What is the difference between accessing vector elements using an iterator vs an index?

前端 未结 4 2109
名媛妹妹
名媛妹妹 2021-02-19 09:29

What advantages are there in accessing vector elements using an iterator vs an index?

4条回答
  •  广开言路
    2021-02-19 09:55

    Modularity is the answer. Suppose you wrap your logic in a function call (a good practice). In that case making it receive iterator will make it generic, so that it can operate on a C style array (pointer), an C++ stl vector or anything really that behaves like an iterator container, such as a linked list for instance.

提交回复
热议问题