|
In computer science, an iterator is an object which 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. Computer science, or computing science, is the study of the theoretical foundations of information and computation and their implementation and application in computer systems. ...
An object is fundamental concept in object-oriented programming. ...
In database packages, the term cursor refers to a control structure for the successive traversal (and potential processing) of records in a result set as returned by a query. ...
This article does not cite any references or sources. ...
Description
An iterator may be thought of as a type of pointer which 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. It has been suggested that Software pointer be merged into this article or section. ...
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. A loop counter is a counting variable used in the loop structure of most computer programming languages. ...
A loop counter is a counting variable used in the loop structure of most computer programming languages. ...
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 advantages. Index has two distinct meanings in computer science: an integer which identifies an array element, and a data structure which enables sublinear-time lookup. ...
A loop counter is a counting variable used in the loop structure of most computer programming languages. ...
- 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. In computer science, random access is the ability to access a random element of a group in equal time. ...
Look up list in Wiktionary, the free dictionary. ...
In computer science, a tree is a widely-used computer data structure that emulates a tree structure with a set of linked nodes. ...
Object-oriented programming (OOP) is a computer programming paradigm in which a software system is modeled as a set of objects that interact with each other. ...
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. This is often manifested by some sort of "foreach" statement, such as in the following examples: Wikibooks has a book on the topic of Perl Programming Perl is a dynamic programming language created by Larry Wall and first released in 1987. ...
Python is a high-level programming language first released by Guido van Rossum in 1991. ...
The title given to this article is incorrect due to technical limitations. ...
Ruby is a reflective, dynamic, object-oriented programming language. ...
Java is a programming language originally developed by Sun Microsystems and released in 1995. ...
Object Pascal is an object oriented derivative of Pascal mostly known as the primary programming language of Borland Delphi. ...
For each (or foreach) is a computer language idiom for traversing items in a collection. ...
# Perl, implicit iteration foreach $val (@list) { print "$valn"; } # Python, implicit iteration for Value in List: print Value // Java, J2SE 5.0, implicit iteration for (Value v : list) System.out.print(v); // C#, implicit iteration foreach (Value v in list) Console.WriteLine(v); // ActionScript, implicit iteration for(Value in Object) { trace(Value+" = "+Object[Value]); } # Ruby, implicit iteration list.each do |value| puts value end // Delphi, implicit iteration for value in list do begin write(value); end; The C++ language also has a std::for_each()[1] function template which allows for similar implicit iteration, but it still requires explicit iterator objects as initial input. It also does not allow the body to be declared inline, requiring a function 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.Lambda to implicitly generate function objects with familiar infix operator syntax. Because it is only a library, however, certain operations have to be done via workarounds. C++ (pronounced see plus plus, IPA: ) is a general-purpose, programming language with high-level and low-level capabilities. ...
Boost is a collection of libraries that extend the functionality of C++. The libraries are licensed under the Boost Software License, a very open license designed to allow Boost to be used with any project. ...
Generators -
One way of implementing iterators is using 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. In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. ...
In computer science, a subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code. ...
A tiling with squares whose sides are successive Fibonacci numbers in length A Fibonacci spiral, created by drawing arcs connecting the opposite corners of squares in the Fibonacci tiling shown above â see golden spiral. ...
Python is a high-level programming language first released by Guido van Rossum in 1991. ...
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a+b 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. C++ (pronounced see plus plus, IPA: ) is a general-purpose, programming language with high-level and low-level capabilities. ...
The Standard Template Library (STL) is a software library included in the C++ Standard Library. ...
C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ...
Pointer arithmetic is a particular arithmetic involving pointers, typical of the C programming language. ...
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. ContainerType C; // Any standard container type, like std::list<sometype> //for (ContainerType::iterator it = C.begin(); it != C.end(); ++it) { for mutable iterator //(if you need to modify elements) for (ContainerType::const_iterator it = C.begin(); it != C.end(); ++it) { std::cout << *it << std::endl; } 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.
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(). Java is a programming language originally developed by Sun Microsystems and released in 1995. ...
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 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 Java 2 Platform, Standard Edition or J2SE is a collection of java Application Programming Interfaces targeting Java platform applications running on a workstation. ...
For each (or foreach) is a computer language idiom for traversing items in a collection. ...
for (MyType obj : list) System.out.print(obj); 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: Python is a high-level programming language first released by Guido van Rossum in 1991. ...
For each (or foreach) is a computer language idiom for traversing items in a collection. ...
In some programming languages, list comprehension is a syntactic construct for creating a list based on existing lists, analogous to the set-builder notation (set comprehension), that is, the mathematical notation such as the following: For an example, in Haskells list comprehension syntax, the example set-builder construct above...
The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted (by both the runtime system and by human readers). ...
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: An associative array (also map, hash, dictionary, finite map, lookup table, and in query-processing an index or index file) is an abstract data type composed of a collection of keys and a collection of values, where each key is associated with one value. ...
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 iteratable sequence type or class, the builtin function iter() is used to create an iterator object. The iterator object then provides a next() method which returns the next element in the container. It will raise a StopIteration error 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() 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. In computer science, an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation. ...
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. PHP is a reflective programming language originally designed for producing dynamic web pages. ...
Wikibooks has a book on the topic of Perl Programming Perl is a dynamic programming language created by Larry Wall and first released in 1987. ...
There are two syntaxes; the second is a minor but useful extension of the first: foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement The first form 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 second form does the same thing, except that the current element's key 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 "rewindingn"; reset($this->var); } public function current() { $var = current($this->var); echo "current: $varn"; return $var; } public function key() { $var = key($this->var); echo "key: $varn"; return $var; } public function next() { $var = next($this->var); echo "next: $varn"; 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. if valid() { 2.1 current() in $value 2.3 key() in $key 2.4 next() } 3. End of Loop. See also The word iteration is sometimes used in everyday English with a meaning virtually identical to repetition. ...
In object-oriented programming, an iterator is an object allowing one to sequence through all of the elements or parts contained in some other object, typically a container or list. ...
In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure. ...
In software engineering (or computer science), a design pattern is a general repeatable solution to a commonly occurring problem in software design. ...
External links Look up Iterator in Wiktionary, the free dictionary. |