Skip to content. Skip to navigation

ICTP Portal

Sections
You are here: Home Manuals on-line PGI Compiler pgC_lib Iterators
Personal tools
Document Actions

Iterators



Click on the banner to return to the class reference home page.

Iterators



Summary

Pointer generalizations for traversal and modification of collections.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Description

Iterators are a generalization of pointers that allow a C++ program to uniformly interact with different data structures. The illustration below displays the five iterator categories defined by the standard library, and shows their hierarchical relationship. Because standard library iterator categories are hierarchical, each category includes all the requirements of the categories above it.

Because iterators are used to traverse and access containers, the nature of the container determines what type of iterator it generates. And, because algorithms require specific iterator types as arguments, it is iterators that, for the most part, determine which standard library algorithms can be used with which standard library containers.

To conform to the C++ standard, all container and sequence classes must provide their own iterators. An instance of a container or sequence's iterator may be declared using either of the following:

class name ::iterator
class name ::const_iterator

Containers and sequences must also provide const iterators to the beginning and end of their collections. These may be accessed using the class members, begin() and end().

The semantics of iterators are a generalization of the semantics of C++ pointers. Every template function that takes iterators will work using C++ pointers for processing typed contiguous memory sequences.

Iterators may be constant or mutable depending upon whether the result of the operator* behaves as a reference or as a reference to a constant. Constant iterators cannot satisfy the requirements of an output_iterator.

Every iterator type guarantees that there is an iterator value that points past the last element of a corresponding container. This value is called the past-the-end value. No guarantee is made that this value is dereferencable.

Every function provided by an iterator is required to be realized in amortized constant time.

Key to Iterator Requirements

The following key pertains to the iterator requirements listed below:

a and b
values of type X
n
value of distance type
u, Distance, tmp and m
identifiers
r
value of type X&
t
value of type T

Requirements for Input Iterators

The following expressions must be valid for input iterators:

X u(a)
copy constructor, u == a
X u = a
assignment, u == a
a == b, a != b
 
  return value convertible to bool
*a
a == b implies *a == *b
a->m
equivalent to (*a).m
++r
returns X&
r++
return value convertible to const X&
*r++
returns type T

For input iterators, a == b does not imply that ++a == ++b.

Algorithms using input iterators should be single pass algorithms. That is they should not pass through the same iterator twice.

The value of type T does not have to be an lvalue.

Requirements for Output Iterators

The following expressions must be valid for output iterators:

X(a)
copy constructor, a == X(a)
X u(a)
copy constructor, u == a
X u = a
assignment, u == a
*a = t
result is not used
++r
returns X&
r++
return value convertible to const X&
*r++ = t
result is not used

The only valid use for the operator* is on the left hand side of the assignment statement.

Algorithms using output iterators should be single pass algorithms. That is they should not pass through the same iterator twice.

Requirements for Forward Iterators

The following expressions must be valid for forward iterators:

X u
u might have a singular value
X()
X() might be singular
X(a)
copy constructor, a == X(a)
X u(a)
copy constructor, u == a
X u = a
assignment, u == a
a == b, a != b
return value convertible to bool
*a
return value convertible to T&
a->m
equivalent to (*a).m
++r
returns X&
r++
return value convertible to const X&
*r++
returns T&

Forward iterators have the condition that a == b implies *a== *b.

There are no restrictions on the number of passes an algorithm may make through the structure.

Requirements for Bidirectional Iterators

A bidirectional iterator must meet all the requirements for forward iterators. In addition, the following expressions must be valid:

--r
returns X&
r--
return value convertible to const X&
*r--
returns T&

Requirements for Random Access Iterators

A random access iterator must meet all the requirements for bidirectional iterators. In addition, the following expressions must be valid:

r += n
Semantics of --r or ++r n times depending on the sign of n
a + n, n + a
returns type X
r -= n
returns X&, behaves as r += -n
a - n
returns type X
b - a
returns Distance
a[n]
*(a+n), return value convertible to T
a < b
total ordering relation
a > b
total ordering relation opposite to <
a <= b
!(a > b)
a >= b
!(a < b)

All relational operators return a value convertible to bool.


©Copyright 1996, Rogue Wave Software, Inc.

Weather
No information available
 

Powered by Plone This site conforms to the following standards: