0% found this document useful (0 votes)
66 views10 pages

Collection

Iterators are objects that allow traversal through the elements of a collection regardless of its implementation. There are different types of iterators such as external iterators and implicit iterators. External iterators have operations for accessing elements and traversing to the next element, while implicit iterators hide the iterator object and allow iteration using language features like foreach loops. Iterators isolate users from the internal structure of collections and allow different access methods like indexing.

Uploaded by

Amin Ganjali
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views10 pages

Collection

Iterators are objects that allow traversal through the elements of a collection regardless of its implementation. There are different types of iterators such as external iterators and implicit iterators. External iterators have operations for accessing elements and traversing to the next element, while implicit iterators hide the iterator object and allow iteration using language features like foreach loops. Iterators isolate users from the internal structure of collections and allow different access methods like indexing.

Uploaded by

Amin Ganjali
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Iterator 1

Iterator
In computer science, an iterator is an object that allows a programmer to traverse through all the elements of a
collection, regardless of its specific implementation. An iterator is sometimes called a cursor, especially within the
context of a database.

Description

External iterators and the iterator pattern


An external iterator may be thought of as a type of pointer that has two primary operations: referencing one
particular element in the object collection (called element access), and modifying itself so it points to the next
element (called element traversal). There must also be a way to create an iterator so it points to some first element as
well as some way to determine when the iterator has exhausted all of the elements in the container. Depending on the
language and intended use, iterators may also provide additional operations or exhibit different behaviors.
The primary purpose of an iterator is to allow a user to process every element of a container while isolating the user
from the internal structure of the container. This allows the container to store elements in any manner it wishes while
allowing the user to treat it as if it were a simple sequence or list. An iterator class is usually designed in tight
coordination with the corresponding container class. Usually the container provides the methods for creating
iterators.
Note that a loop counter is sometimes also referred to as a loop iterator. A loop counter, however, only provides the
traversal functionality and not the element access functionality.

Generators
One way of implementing iterators is to use a special kind of subroutine, known as a generator, that can yield values
to its caller multiple times (instead of returning just once). Most iterators are naturally expressible as generators, but
because generators preserve their local state between invocations, they're particularly well-suited for complicated,
stateful iterators, such as tree traversers. An example of a generator returning the Fibonacci numbers using Python's
yield statement can be seen below.

def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a+b

for number in fibonacci(): # Use the generator as an iterator


print number
Iterator 2

Implicit iterators
Some object-oriented languages such as Perl, Python, C#, Ruby and later versions of Java and Delphi provide an
intrinsic way of iterating through the elements of a container object without the introduction of an explicit iterator
object. An actual iterator object may exist in reality, but if it does it is not exposed within the source code of the
language.
Implicit iterators are often manifested by a "foreach" statement (or equivalent), such as in the following Python
example:

for value in iterable:


print value

Or other times they may be created by the collection object itself, as in this Ruby example:

iterable.each do |value|
puts value
end

This iteration style is sometimes called "internal iteration" because its code fully executes within the context of the
iterable object (which controls all aspects of iteration), and the programmer only provides the operation to execute at
each step (using an anonymous function).
Languages that support list comprehensions or similar constructs may also make use of implicit iterators during the
construction of the result list, as in Python:

names = [person.name for person in roster if person.male]

Sometimes the implicit hidden nature is only partial. The C++ language has a few function templates, such as
for_each(), that allow for similar implicit iteration. However they still require explicit iterator objects as their initial
input. But once initialized the subsequent iteration happens implicitly without the continued use of any exposed
iterator object.

Contrasting with indexing


In procedural languages it is common to use indexing based on a loop counter to loop through all the elements in a
sequence such as an array. Although indexing may also be used with some object-oriented containers, the use of
iterators may have some advantages:
• Counting loops are not suitable to all data structures, in particular to data structures with no or slow random
access, like lists or trees.
• Iterators can provide a consistent way to iterate on data structures of all kinds, and therefore make the code more
readable, reusable, and less sensitive to a change in the data structure.
• An iterator can enforce additional restrictions on access, such as ensuring that elements can not be skipped or that
a previously visited element can not be accessed a second time.
• An iterator may allow the container object to be modified without invalidating the iterator. For instance, once an
iterator has advanced beyond the first element it may be possible to insert additional elements into the beginning
of the container with predictable results. With indexing this is problematic since the index numbers must change.
The ability of a container to be modified while iterating through its elements has become necessary in modern
object-oriented programming, where the interrelationships between objects and the effects of operations may not be
obvious. By using an iterator one is isolated from these sorts of consequences.
Iterator 3

Iterators in different programming languages

C++
The C++ language makes wide use of iterators in its Standard Template Library, which provides several different
kinds of iterators, including forward iterators, bidirectional iterators, and random access iterators. All of the
standard container template types provide a rich and consistent set of iterator types. The syntax of standard iterators
is designed to resemble that of ordinary C pointer arithmetic, where the * and -> operators are used to reference the
element to which the iterator points, and pointer arithmetic operators like ++ are used to advance the iterator to the
next element.
Iterators are usually used in pairs, where one is used for the actual iteration and the second serves to mark the end of
the collection. The iterators are created by the corresponding container class using standard methods such as begin()
and end(). The iterator returned by begin() points to the first element, while the iterator returned by end() is a special
value that does not reference any element.
When an iterator is advanced beyond the last element it is by definition equal to the special end iterator value. The
following example shows a typical use of an iterator.

std::vector<int> items;
items.push_back(1); //Add integer '1' to the back of vector 'items'
items.push_back(2); //Add integer '2' to the back of vector 'items'
items.push_back(3); //Add integer '3' to the back of vector 'items'

for (std::vector<int>::iterator i = items.begin(); i != items.end(); ++i) {


//Iterate through 'items'
std::cout << *i; //And print current index of 'items'
}

//Prints 123

There are many varieties of iterators each with slightly different behavior, including: forward, reverse, and
bidirectional iterators; random-access iterators; input and output iterators; and const iterators (which protect the
container or its elements from modification). However not every type of container supports every type of iterator. It
is possible for users to create their own iterator types by deriving subclasses from the standard std::iterator class
template.
Iterator safety is defined separately for the different types of standard containers, in some cases the iterator is very
permissive in allowing the container to change while iterating.
Implicit iteration is also partially supported by C++ through the use of standard function templates, such as
std::for_each() [1], std::copy() [2] and std::accumulate() [3]. When used they must be initialized with existing iterators,
usually begin and end, that define the range over which iteration occurs. But no explicit iterator object is
subsequently exposed as the iteration proceeds. This example shows the use of for_each.

ContainerType<ItemType> C; // Any standard container type of ItemType elements


void ProcessItem( const ItemType& I ) // Function which will process
each item of the collection
{
std::cout << I << '\n';
}

std::for_each(C.begin(), C.end(), ProcessItem); // A for-each


Iterator 4

iteration loop

The same can be achieved using std::copy and std::ostream_iterator [4]:

std::copy(C.begin(), C.end(), std::ostream_iterator<ItemType>(std::cout, "\n"));

A limitation is that this technique does not allow the body of the for-each loop to be declared inline, requiring a
function pointer or function object to be declared elsewhere and passed as an argument. This can be partially
compensated for by using a library such as Boost and using lambda to implicitly generate function objects with
familiar infix operator syntax. Due to it only being a library, however, certain operations have to be done via
workarounds.
The next revision of C++, known as C++0x, will natively support lambda function syntax, allowing the function
template body to be declared inline.

C# and other .NET languages


Iterators in the .NET Framework are called "enumerators" and represented by the IEnumerator interface.
IEnumerator provides a MoveNext() method, which advances to the next element and indicates whether the end of
the collection has been reached; a Current property, to obtain the value of the element currently being pointed at; and
an optional Reset() method, to rewind the enumerator back to its initial position. The enumerator initially points to a
special value before the first element, so a call to MoveNext() is required to begin iterating.
Enumerators are typically obtained by calling the GetEnumerator() method of an object implementing the
IEnumerable interface. Container classes typically implement this interface. However, the foreach statement in C#
can operate on any object providing such a method, even if it doesn't implement IEnumerable. Both interfaces were
expanded into generic versions in .NET 2.0.
The following shows a simple use of iterators in C# 2.0:

// explicit version
IEnumerator<MyType> iter = list.GetEnumerator();
while (iter.MoveNext())
Console.WriteLine(iter.Current);

// implicit version
foreach (MyType value in list)
Console.WriteLine(value);

C# 2.0 also supports generators: a method which is declared as returning IEnumerator (or IEnumerable), but uses the
"yield return" statement to produce a sequence of elements instead of returning an object instance, will be
transformed by the compiler into a new class implementing the appropriate interface.

Java
Introduced in the Java JDK 1.2 release, the java.util.Iterator interface allows the iteration of container classes. Each
Iterator provides a next() and hasNext() method, and may optionally support a remove() method. Iterators are created
by the corresponding container class, typically by a method named iterator().
The next() method advances the iterator and returns the value pointed to by the iterator. When first created, an
iterator points to a special value before the first element, so that the first element is obtained upon the first call to
next(). To determine when all the elements in the container have been visited the hasNext() test method is used. The
following example shows a simple use of iterators:
Iterator 5

Iterator iter = list.iterator();


//Iterator<MyType> iter = list.iterator(); in J2SE 5.0
while (iter.hasNext()){
System.out.println(iter.next());
}

For collection types which support it, the remove() method of the iterator removes the most recently visited element
from the container. Most other types of modification to the container while iterating are unsafe.
Additionally, for java.util.List there is a java.util.ListIterator with a similar API but that allows forward and
backward iteration, provides its current index in the list and allows setting of the list element at its position.
The J2SE 5.0 release of Java introduced the Iterable interface to support an enhanced for (foreach) loop for iterating
over collections and arrays. Iterable defines the iterator() method that returns an Iterator. Using the enhanced for
loop, the preceding example can be rewritten as

for (MyType obj : list){


System.out.print(obj);
}

Ruby
Ruby implements iterators quite differently; all iterations are done by means of passing callback closures to
container methods - this way Ruby not only implements basic iteration but also several patterns of iteration like
function mapping, filters and reducing. Ruby also supports an alternative syntax for the basic iterating method each,
the following three examples are equivalent:

(0...42).each do |n|
puts n
end

...and...

for n in 0...42
puts n
end

or even shorter

42.times do |n|
puts n
end

Ruby can also iterate over fixed lists by using Enumerators and either calling their #next method or doing a for each
on them, as above.
Iterator 6

Python
Iterators in Python are a fundamental part of the language and in many cases go unseen as they are implicitly used in
the for (foreach) statement, in list comprehensions, and in generator expressions. All of Python's standard built-in
sequence types support iteration, as well as many classes which are part of the standard library. The following
example shows typical implicit iteration over a sequence:

for value in sequence:


print(value)

Python dictionaries (a form of associative array) can also be directly iterated over, when the dictionary keys are
returned; or the items method of a dictionary can be iterated over where it yields corresponding key,value pairs as a
tuple:

for key in dictionary:


value = dictionary[key]
print(key, value)

for key, value in dictionary.items():


print(key, value)

Iterators however can be used and defined explicitly. For any iterable sequence type or class, the built-in function
iter() is used to create an iterator object. The iterator object can then be iterated with the next() function, which uses
the __next__() method internally, which returns the next element in the container. (The previous statement applies to
Python 3.x. In Python 2.x, the next() method is equivalent.) A StopIteration exception will be raised when no more
elements are left. The following example shows an equivalent iteration over a sequence using explicit iterators:

it = iter(sequence)
while True:
try:
value = it.next() # in Python 2.x
value = next(it) # in Python 3.x
except StopIteration:
break
print(value)

Any user-defined class can support standard iteration (either implicit or explicit) by defining an __iter__() method
which creates an iterator object. The iterator object then needs to define a __next__() method which returns the next
element.
Python's generators implement this iteration protocol.

PHP
PHP 4 introduced a foreach construct, much like Perl and some other languages. This simply gives an easy way to
iterate over arrays. foreach works only on arrays in PHP 4, and will issue an error when you try to use it on a
variable with a different data type or an uninitialized variable.
In PHP 5, foreach is allowed on object iterating through all the public members.
There are two syntaxes; the second is a minor but useful extension of the first.
Example A

<?php
foreach (array_expression as $value)
Iterator 7

echo "$value\n";
?>

Example B

<?php
foreach (array_expression as $key => $value)
echo "($key)$value\n";
?>

The Example A loops over the array given by array_expression. On each loop, the value of the current element is
assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next
element).
The Example B has the same functionality as above. Additionally, the current element's key (in this case,
array_expression) will be assigned to the variable $key on each loop.
The Iterator interface is pre-defined in PHP 5 and objects can be customized to handle iteration.

<?php
class MyIterator implements Iterator
{
private $var = array();

public function __construct($array)


{
if (is_array($array)) {
$this->var = $array;
}
}

public function rewind() {


echo "rewinding\n";
reset($this->var);
}

public function current() {


$var = current($this->var);
echo "current: $var\n";
return $var;
}

public function key() {


$var = key($this->var);
echo "key: $var\n";
return $var;
}

public function next() {


Iterator 8

$var = next($this->var);
echo "next: $var\n";
return $var;
}

public function valid() {


$var = $this->current() !== false;
echo "valid: {$var}\n";
return $var;
}
}
?>

These methods are all being used in a complete foreach($obj AS $key=>$value) sequence.
The methods of Iterators are executed in the following order:
1. rewind()
2. while valid()
{
2.1 current() in $value
2.3 key() in $key
2.4 next()
}

MATLAB
MATLAB supports both external and internal implicit iteration using either "native" arrays or cell arrays. In the case
of external iteration where the onus is on the user to advance the traversal and request next elements, one can define
a set of elements within an array storage structure and traverse the elements using the for-loop construct. For
example,

% Define an array of integers


myArray = [1,3,5,7,11,13];

for n = myArray
% ... do something with n
disp(n) % Echo integer to Command Window
end

traverses an array of integers using the for keyword.


In the case of internal iteration where the user can supply an operation to the iterator to perform over every element
of a collection, many built-in operators and MATLAB functions are overloaded to execute over every element of an
array and return a corresponding output array implicitly. Furthermore, the arrayfun and cellfun functions can be
leveraged for performing custom or user defined operations over "native" arrays and cell arrays respectively. For
example,

function simpleFun
% Define an array of integers
myArray = [1,3,5,7,11,13];
Iterator 9

% Perform a custom operation over each element


myNewArray = arrayfun(@(a)myCustomFun(a),myArray);

% Echo resulting array to Command Window


myNewArray

function outScalar = myCustomFun(inScalar)


% Simply multiply by 2
outScalar = 2*inScalar;

defines a primary function simpleFun which implicitly applies custom subfunction myCustomFun to each element of
an array using built-in function arrayfun.
Alternatively, it may be desirable to abstract the mechanisms of the array storage container from the user by defining
a custom object-oriented MATLAB implementation of the Iterator Pattern. Such an implementation supporting
external iteration is demonstrated in MATLAB Central File Exchange item Design Pattern: Iterator (Behavioural) [5].
This is written in the new class-definition syntax introduced with MATLAB software version 7.6 (R2008a) [6] and
features a one-dimensional cell array realisation of the List Abstract Data Type (ADT) as the mechanism for storing
a heterogeneous (in data type) set of elements. It provides the functionality for explicit forward List traversal with
the hasNext(), next() and reset() methods for use in a while-loop.

References
[1] http:/ / www. sgi. com/ tech/ stl/ for_each. html
[2] http:/ / www. sgi. com/ tech/ stl/ copy. html
[3] http:/ / www. sgi. com/ tech/ stl/ accumulate. html
[4] http:/ / www. sgi. com/ tech/ stl/ ostream_iterator. html
[5] http:/ / www. mathworks. com. au/ matlabcentral/ fileexchange/ 25225
[6] "New Class-Definition Syntax Introduced with MATLAB Software Version 7.6" (http:/ / www. mathworks. com/ access/ helpdesk/ help/
techdoc/ matlab_oop/ brqzfth-1. html#brqzfth-3). The MathWorks, Inc. March 2009. . Retrieved September 22, 2009.

External links
• Article " Understanding and Using Iterators (http://www.perl.com/pub/a/2005/06/16/iterators.html)" by
Joshua Gatcomb
• Article " A Technique for Generic Iteration and Its Optimization (http://www.csd.uwo.ca/~watt/pub/reprints/
2006-wgp-jflow.pdf)" (217 KB) by Stephen M. Watt
• STL Iterators (http://www.cprogramming.com/tutorial/stl/iterators.html)
• What are iterators? (http://www.phpro.org/tutorials/Introduction-to-SPL.html#2) - Reference description
(http://www.php.net/~helly/php/ext/spl/interfaceIterator.html#_details)
• Java interface (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Iterator.html)
• .NET interface (http://msdn.microsoft.com/en-us/library/78dfe2yb.aspx)
• Template reference (http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/structstd_1_1iterator.html)
• Boost C++ Iterator Library (http://boost.org/libs/iterator/doc/index.html)
• PHP: Object Iteration (http://us3.php.net/manual/en/language.oop5.iterations.php)
Article Sources and Contributors 10

Article Sources and Contributors


Iterator  Source: http://en.wikipedia.org/w/index.php?oldid=402895061  Contributors: 16@r, A scientist, Abednigo, AdSR, Ahoerstemeier, Balabiot, Beland, Bnedelko, Boredzo, Bos Gaurus,
Btx40, Camhusmj38, CatherineMunro, Chip Zero, DNewhall, Dc987, Dmeranda, Doug Bell, E946, Eismcsquare, Elysdir, EngineerScotty, Fagstein, Flyingped, Fredrik, Furrykef, FvdP,
GRAHAMUK, Googl, Icairns, Ilario, Imroy, Ipsign, Jaberwocky6669, JeepdaySock, Jitse Niesen, Jorend, Kku, Kuru, LeeG, LeeHunter, Luís Felipe Braga, Matěj Grabovský, Mav, Mintleaf,
Mr2001, Mskfisher, NoJr0xx, Ogmios, Orderud, Ospalh, Otaviogood, Paddy3118, Peaceoutside, Pete142, Piet Delport, Raubsaurier, Reach Out to the Truth, Requiem18th, Rufous, Sandrarossi,
SickTwist, SixStrings117, Spoon!, TakuyaMurata, Tarquin, Torc2, Unyoyega, Vdcappel, Wikidrone, Winxa, 112 anonymous edits

License
Creative Commons Attribution-Share Alike 3.0 Unported
http:/ / creativecommons. org/ licenses/ by-sa/ 3. 0/

You might also like