Iterators date to the CLU programming language in 1974.
The following points apply to all collections, defined in the library or by the user: This rule does not imply that indices should be cheap to convert to actual integers.
The offsets for consecutive elements could be non-consecutive (e.g., in a hash table with open addressing), or consist of multiple offsets so that the conversion to an integer is non-trival (e.g., in a tree).
In C , validity of an iterator is a property of the iterator itself, since iterators can be dereferenced to access collection elements.
In Swift, in order to access a collection element designated by an index, subscript operator is applied to the collection, .
Note that an iterator performs traversal and also gives access to data elements in a container, but does not perform iteration (i.e., not without some significant liberty taken with that concept or with trivial use of the terminology).
An iterator is behaviorally similar to a database cursor.
An index that is valid for a certain collection designates an element of that collection or represents a one-past-end index.
Operations that access collection elements require valid indexes (this includes accessing using the subscript operator, slicing, swapping elements, removing elements etc.) Using an invalid index to access elements of a collection leads to unspecified memory-safe behavior.
In procedural languages it is common to use the subscript operator and 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: 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.
Several languages, such as Perl and Python, implement streams as iterators.