外部的迭代器可以被认为是某种类型的指针,它有两个主要操作:引用在一个对象搜集(英语:Collection (abstract data type))(collection)中的一个特定元素(称为元素访问),和修改自身使其指向下一个元素(称为元素遍历)[4]。还必须有一种方式建立迭代器并指向容器的第一个元素,还要有某种方式确定何时迭代器已经穷尽了容器中所有的元素。依据语言和意向用途,迭代器还可以提供额外的操作或展示不同的行为。
// Method that takes an iterable input (possibly an array)// and returns all even numbers.publicstaticIEnumerable<int>GetEven(IEnumerable<int>numbers){foreach(intiinnumbers){if(i%2==0)yieldreturni;}}
^Gatcomb, Joshua. Understanding and Using Iterators. Perl.com. [2012-08-08]. (原始内容存档于2005-06-30). A user-defined iterator usually takes the form of a code reference that, when executed, calculates the next item in a list and returns it. When the iterator reaches the end of the list, it returns an agreed-upon value.
^ 2.02.1Watt, Stephen M. A Technique for Generic Iteration and Its Optimization(PDF). The University of Western Ontario, Department of Computer Science. [2012-08-08]. (原始内容存档于2012-08-06). Iterators were introduced as constructs to allow looping over abstract data structures without revealing their internal representation.
^Alex Allain. STL Iterators. Cprogramming.com - Your resource for C and C++. [2012-08-08]. (原始内容存档于2021-02-13). You can think of an iterator as pointing to an item that is part of a larger container of items.
^ 4.04.1Difference between an external iterator and an internal iterator. CareerRide.COM. 2009-04-03 [2012-08-08]. (原始内容存档于2021-04-18). An internal iterator is implemented by the member functions of the class which has the iteration logic. An external iterator is implemented by a separate class which can be attached to the object which has iteration logic. The advantage of external iterator is that, many iterators can be made active simultaneously on the existing or same object.
^Watt, Stephen M. A Technique for Generic Iteration and Its Optimization(PDF). The University of Western Ontario, Department of Computer Science. [2012-08-08]. (原始内容存档(PDF)于2022-05-26). Some authors use the term iterator, and others the term generator. Some make subtle distinctions between the two.
^concurrent_unordered_set Template Class. Intel Threading Building Blocks for Open Source. [2012-08-09]. (原始内容存档于2015-05-01). •The iterator types iterator and const_iterator are of the forward iterator category