.\" ident @(#)Iterators.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH Iterators 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2Iterators\fP \ - Pointer generalizations for traversal and modification of collections. .SH 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 the type of iterator it generates. Also, 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 include their own iterator types. Each iterator may be a class defined within the container or may be a simple pointer, whichever is appropriate. Containers and sequences must also include \f2const\fP iterators that point to the beginning and end of their collections. These may be accessed using the class members, \f2begin()\fP and \f2end()\fP. Because the semantics of iterators are a generalization of the semantics of C++ pointers, every template function that takes iterators also works using C++ pointers to contiguous memory sequences. For example, both of the following uses of the generic algorithm \f2count\fP are valid: .br list 1; .br count(1.begin(), 1.end()); .br int buf[4]={1,2,3,4}; .br count(buf, buf+4); Iterators may be constant or mutable depending upon whether the result of the \f2operator* \fPbehaves as a reference or as a reference to a constant. Constant iterators cannot satisfy the requirements of an \f2output_iterator\fP. 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 dereferenceable. Every function included in an iterator is required to be realized in amortized constant time. .SH KEY TO ITERATOR REQUIREMENTS The following key pertains to the iterator requirements listed below: .HP 10 \f2a and b \fPvalues of type \f2X\fP .HP 0 .HP 4 \f2n \fPvalue representing a \f2distance\fP between two iterators .HP 0 .HP 25 \f2u, Distance, tmp and m \fPidentifiers .HP 0 .HP 4 \f2r \fPvalue of type \f2X&\fP .HP 0 .HP 4 \f2t \fPvalue of type \f2T\fP .HP 0 .SH REQUIREMENTS FOR INPUT ITERATORS The following expressions must be valid for input iterators: .HP 9 \f2X u(a) \fPcopy constructor, \f2u == a\fP .HP 0 .HP 10 \f2X u = a \fPassignment, \f2u == a \fP .HP 0 .HP 17 \f2a == b, a != b \fPreturn value convertible to \f2bool\fP .HP 0 .HP 5 \f2*a a == b\fP implies \f2*a == *b\fP .HP 0 .HP 7 \f2a->m \fPequivalent to \f2(*a).m\fP .HP 0 .HP 6 \f2++r \fPreturns \f2X&\fP .HP 0 .HP 6 \f2r++ \fPreturn value convertible to const \f2X&\fP .HP 0 .HP 7 \f2*r++ \fPreturns type \f2T\fP .HP 0 For input iterators, \f2a == b\fP does not imply that\f2 ++a == ++b\fP. Algorithms using input iterators should be single pass algorithms. They should not pass through the same iterator twice. The value of type \f2T\fP does not have to be an \f2lvalue\fP. .SH REQUIREMENTS FOR OUTPUT ITERATORS The following expressions must be valid for output iterators: .HP 7 \f2X(a) \fPcopy constructor, \f2a == X(a)\fP .HP 0 .HP 9 \f2X u(a) \fPcopy constructor, \f2u == a\fP .HP 0 .HP 10 \f2X u = a \fPassignment, \f2u == a\fP .HP 0 .HP 9 \f2*a = t \fPresult is not used .HP 0 .HP 6 \f2++r \fPreturns \f2X&\fP .HP 0 .HP 6 \f2r++ \fPreturn value convertible to \f2const\fP \f2X&\fP .HP 0 .HP 11 \f2*r++ = t \fPresult is not used .HP 0 The only valid use for the \f2operator* \fPis on the left hand side of the assignment statement. Algorithms using output iterators should be single pass algorithms. They should not pass through the same iterator twice. .SH REQUIREMENTS FOR FORWARD ITERATORS The following expressions must be valid for forward iterators: .HP 6 \f2X u u\fP might have a singular value .HP 0 .HP 6 \f2X() X()\fP might be singular .HP 0 .HP 7 \f2X(a) \fPcopy constructor, \f2a == X(a)\fP .HP 0 .HP 9 \f2X u(a) \fPcopy constructor, \f2u == a\fP .HP 0 .HP 10 \f2X u = a \fPassignment, \f2u == a\fP .HP 0 .HP 17 \f2a == b, a != b \fPreturn value convertible to \f2bool\fP .HP 0 .HP 5 \f2*a \fPreturn value convertible to \f2T&\fP .HP 0 .HP 7 \f2a->m \fPequivalent to \f2(*a).m\fP .HP 0 .HP 6 \f2++r \fPreturns \f2X&\fP .HP 0 .HP 6 \f2r++ \fPreturn value convertible to \f2const\fP \f2X&\fP .HP 0 .HP 7 \f2*r++ \fPreturns \f2T&\fP .HP 0 Forward iterators have the condition that \f2a == b\fP implies \f2*a== *b\fP. There are no restrictions on the number of passes an algorithm may make through the structure. .SH REQUIREMENTS FOR BIDIRECTIONAL ITERATORS A bidirectional iterator must meet all the requirements for forward iterators. In addition, the following expressions must be valid: .HP 6 \f2--r \fPreturns \f2X&\fP .HP 0 .HP 6 \f2r-- \fPreturn value convertible to \f2const\fP \f2X&\fP .HP 0 .HP 7 \f2*r-- \fPreturns \f2T\fP .HP 0 .SH 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: .HP 9 \f2r += n \fPSemantics of \f2--r\fP or \f2++r n\fP times depending on the sign of n .HP 0 .HP 15 \f2a + n, n + a \fPreturns type \f2X\fP .HP 0 .HP 9 \f2r -= n \fPreturns \f2X&\fP, behaves as \f2r += -n\fP .HP 0 .HP 8 \f2a - n \fPreturns type \f2X\fP .HP 0 .HP 8 \f2b - a \fPreturns \f2distance\fP .HP 0 .HP 7 \f2a[n] *(a+n)\fP, return value convertible to \f2T\fP .HP 0 .HP 8 \f2a < b \fPtotal ordering relation .HP 0 .HP 8 \f2a > b \fPtotal ordering relation opposite to \f2<\fP .HP 0 .HP 9 \f2a <= b !(a > b)\fP .HP 0 .HP 9 \f2a >= b !(a < b)\fP .HP 0 All relational operators return a value convertible to \f2bool\fP.