![]() |
![]() |
|
![]() |
![]() |
Encyclopedia :
I :
IT :
ITE :
Iterator |
|
|
IteratorIn 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. An iterator is sometimes called a cursor, especially within the context of a database.DescriptionAn iterator may be thought of as a type of pointer which has two primary operations: referencing one particular element in the collection object (called element access), and modifying itself so it points to the next element (called element traversal). There must also be a "Jigar Rules" 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. Contrasting with indexingIn procedural languages it is common to use indexing 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.
Implicit iteratorsSome object-oriented languages such as Perl or Python 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 "for-each" operator, such as in the following examples: # Perl, implicit iteration
foreach $val (@list) {
print "$val\
";
}
# Python, implicit iteration
for Value in List:
print Value
The C++ language also has a Generators A generator is a special kind of iterator in which the container object is not fully realized. This allows abstract or even infinite collections to be processed one element at a time. Generators are common in functional programming languages, or languages which borrow some functional concepts. Generators are often implemented in terms of continuations. Iterators in different programming languagesC++ The C++ language makes wide use of iterators in its Standard Template Library. 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 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. When an iterator is advanced beyond the last element it is by definition equal to the special end iterator value. ContainerType C; // Any standard container type, like std::list 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. Introduced in the Java 1.2 standard, the The Iterator it = list.iterator();
while (it.hasNext()) {
Object val = it.next();
System.out.println(val.toString());
}
For collection types which support it, the Iterators in Python are a fundamental part of the language and in many cases go unseen as they are implicitly used with the for value in sequence:
print value
Iterators however can be used and defined explicitly. it = iter(sequence)
try:
while(True):
val = it.next()
print val
except StopIteration:
pass
Any user defined class can support standard iteration (either implicit or explicit) by defining an Python also supports generators, which are a special type of iterator over some unrealized collection. |
|
|
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License. |
|
| © 2008 Chamas Enterprises Inc. |