Why iterator doesn't have any reset method?

前端 未结 5 1286
星月不相逢
星月不相逢 2020-12-29 19:52

Why? And what the best way to move iterator items pointer to the first position?

5条回答
  •  时光取名叫无心
    2020-12-29 20:33

    Why?

    Because if you force iterator to have a reset method every iterator has to have a reset method. That gives every iterator writer extra work. Plus some iterators are really hard (or really expensive) to reset, and you wouldn't want users to call reset on them. Iterators over files or streams are good examples.

    what the best way to move iterator items pointer to the first position?

    Create a new iterator. It's rarely more expensive than the reset.

提交回复
热议问题