Internet
Fact-checked

At WiseGEEK, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.

Learn more...

What is an Iterator?

Jessica Susan Reuter
Jessica Susan Reuter

An iterator is a computer language construct that allows a program to read through a group of data values or pieces of information in a systematic way. Iterators also allow for individual access to each member of the group, without affecting the rest of the group. They are used in many scripting and programming languages, including C++, Java, PHP, and Perl. Their implementation is independent of the objects they scan, so they can scan any type of a group of objects.

Traveling through a group of objects with an iterator is usually called iterating or traversing. In most computer languages, iterators are used to traverse groups of related objects called collections. Their implementations are very flexible and able to be used with collections of any size. They are, in general, safer than simple element access in a collection because their range is restricted to the elements in the collection. While it is still possible to access an out-of-bounds index, it is not likely if the iterator is used correctly.

Iterators are used in many scripting and programming languages, including C++.
Iterators are used in many scripting and programming languages, including C++.

Iterators are defined, initialized, and handled in memory differently depending on the computer language in which they are used. The general syntax for them, by contrast, is similar in most languages. There are two different types of syntax which can be used for iterators, defined as explicit or implicit.

Explicit iterators involve the creation of an iterator object, and that object is then used in a traversal loop over the elements of a collection. Implicit iterators involve a special type of loop structure that creates the iterator object without it being specified beforehand. Whether or not explicit or implicit iterators are used for any particular traversal usually depends on the programmer's preference, as differences in the amount of code or performance between them are negligible. The biggest difference between explicit and implicit iterators is that explicit iterators show the creation of an iterator in source code, while implicit iterators do not show this.

Whenever a collection of objects is available, an iterator can be used to scan them. Depending on the capabilities of a computer language, iterators can be used on arrays, maps, lists, and a variety of other collection types. Iterators are a relatively ubiquitous feature of programming languages, and most common languages have some type of functionality available. Some computer languages allow iterators with additional features, like restrictions on the number of times an element is accessed. Since these features are not universal, there are sometimes ways in which they can be implemented independently of the iterators themselves.

Discuss this Article

Post your comments
Login:
Forgot password?
Register:
    • Iterators are used in many scripting and programming languages, including C++.
      By: ビッグアップジャパン
      Iterators are used in many scripting and programming languages, including C++.