What is the difference between an Iterator and a Generator?

后端 未结 9 1801
感情败类
感情败类 2020-12-07 15:49

What is the difference between an Iterator and a Generator?

9条回答
  •  执笔经年
    2020-12-07 16:14

    An iterator is commonly used to move through a collection of items. Often having MoveNext() and Current() methods. MoveNext() would shift the pointer to the next collection item (if possible) and return true/false based on success. Current() would provide the actual value.

    A generator is an implementation of iterator, but instead of pointing to a pre-existing collection, it creates new items on each MoveNext() call.

提交回复
热议问题