1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
<HTML>
<!--
-- Copyright (c) 1996-1999
-- Silicon Graphics Computer Systems, Inc.
--
-- Permission to use, copy, modify, distribute and sell this software
-- and its documentation for any purpose is hereby granted without fee,
-- provided that the above copyright notice appears in all copies and
-- that both that copyright notice and this permission notice appear
-- in supporting documentation. Silicon Graphics makes no
-- representations about the suitability of this software for any
-- purpose. It is provided "as is" without express or implied warranty.
--
-- Copyright (c) 1994
-- Hewlett-Packard Company
--
-- Permission to use, copy, modify, distribute and sell this software
-- and its documentation for any purpose is hereby granted without fee,
-- provided that the above copyright notice appears in all copies and
-- that both that copyright notice and this permission notice appear
-- in supporting documentation. Hewlett-Packard Company makes no
-- representations about the suitability of this software for any
-- purpose. It is provided "as is" without express or implied warranty.
--
-->
<Head>
<Title>Iterator Tags</Title>
<!-- Generated by htmldoc -->
</HEAD>
<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
ALINK="#ff0000">
<IMG SRC="CorpID.gif"
ALT="SGI" HEIGHT="43" WIDTH="151">
<!--end header-->
<BR Clear>
<H1>Iterator Tags</H1>
<Table CellPadding=0 CellSpacing=0 width=100%>
<TR>
<TD Align=left><Img src = "iterators.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD>
<TD Align=right><Img src = "overview.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD>
</TR>
<TR>
<TD Align=left VAlign=top><b>Category</b>: iterators</TD>
<TD Align=right VAlign=top><b>Component type</b>: overview</TD>
</TR>
</Table>
<h3>Summary</h3>
Iterator tag functions are a method for accessing information that is
associated with iterators. Specifically, an iterator type must, as
discussed in the <A href="InputIterator.html">Input Iterator</A> requirements, have an
associated <i>distance type</i> and <i>value type</i>. <A href="#1">[1]</A> It is sometimes
important for an algorithm parameterized by an iterator type to be
able to determine the distance type and value type. Iterator tags
also allow algorithms to determine an iterator's category, so that
they can take different actions depending on whether an iterator is an
<A href="InputIterator.html">Input Iterator</A>, <A href="OutputIterator.html">Output Iterator</A>, <A href="ForwardIterator.html">Forward Iterator</A>,
<A href="BidirectionalIterator.html">Bidirectional Iterator</A>, or <A href="RandomAccessIterator.html">Random Access Iterator</A>.
<P>
Note that the iterator tag functions <tt><A href="distance_type.html">distance_type</A></tt>,
<tt><A href="value_type.html">value_type</A></tt>, and <tt><A href="iterator_category.html">iterator_category</A></tt> are an older method of
accessing the type information associated with iterators: they were
defined in the original STL. The draft C++ standard, however, defines
a different and more convenient mechanism: <tt><A href="iterator_traits.html">iterator_traits</A></tt>.
Both mechanisms are supported <A href="#2">[2]</A>, for reasons of backwards
compatibility, but the older mechanism will eventually be
removed.
<h3>Description</h3>
The basic idea of the iterator tag functions, and of
<tt><A href="iterator_traits.html">iterator_traits</A></tt>, is quite simple: iterators have associated type
information, and there must be a way to access that information.
Specifically, iterator tag functions and <tt><A href="iterator_traits.html">iterator_traits</A></tt> are
used to determine an iterator's value type, distance type, and
iterator category.
<P>
An iterator's <i>category</i> is the most specific concept that it is a
model of: <A href="InputIterator.html">Input Iterator</A>, <A href="OutputIterator.html">Output Iterator</A>, <A href="ForwardIterator.html">Forward Iterator</A>,
<A href="BidirectionalIterator.html">Bidirectional Iterator</A>, or <A href="RandomAccessIterator.html">Random Access Iterator</A>.
This information is expressed in the C++ type system by defining five
category tag types, <tt><A href="input_iterator_tag.html">input_iterator_tag</A></tt>,
<tt><A href="output_iterator_tag.html">output_iterator_tag</A></tt>, <tt><A href="forward_iterator_tag.html">forward_iterator_tag</A></tt>,
<tt><A href="bidirectional_iterator_tag.html">bidirectional_iterator_tag</A></tt>, and
<tt><A href="random_access_iterator_tag.html">random_access_iterator_tag</A></tt>, each of which corresponds to one of
those concepts. <A href="#3">[3]</A>
<P>
The function <tt><A href="iterator_category.html">iterator_category</A></tt> takes a single argument, an
iterator, and returns the tag corresponding to that iterator's
category. That is, it returns a <tt><A href="random_access_iterator_tag.html">random_access_iterator_tag</A></tt> if
its argument is a pointer, a <tt><A href="bidirectional_iterator_tag.html">bidirectional_iterator_tag</A></tt> if its
argument is a <tt><A href="List.html">list</A>::iterator</tt>, and so on. <tt>Iterator_traits</tt>
provides the same information in a slightly different way: if <tt>I</tt> is
an iterator, then <tt><A href="iterator_traits.html">iterator_traits</A><I>::iterator_category</tt> is a nested
<tt>typedef</tt>: it is one of the five category tag types.
<P>
An iterator's <i>value type</i> is the type of object that is returned
when the iterator is dereferenced. (See the discussion in the
<A href="InputIterator.html">Input Iterator</A> requirements.) Ideally, one might want
<tt><A href="value_type.html">value_type</A></tt> to take a single argument, an iterator, and return
the iterator's value type. Unfortunately, that's impossible:
a function must return an object, and types aren't objects. Instead,
<tt><A href="value_type.html">value_type</A></tt> returns the value <tt>(T*) 0</tt>, where <tt>T</tt> is the
argument's value type. The <tt>iterator_traits</tt> class, however,
does not have this restriction: <tt>iterator_traits<I>::value_type</tt>
is a type, not a value. It is a nested <tt>typedef</tt>, and it can be
used in declarations of variables, as an function's argument type or
return type, and in any other ways that C++ types can be used.
<P>
(Note that the function <tt><A href="value_type.html">value_type</A></tt> need
not be defined for <A href="OutputIterator.html">Output Iterators</A>, since an <A href="OutputIterator.html">Output Iterator</A>
need not have a value type. Similarly, <tt><A href="iterator_traits.html">iterator_traits</A><I>::value_type</tt>
is typically defined as <tt>void</tt> when <tt>I</tt> is an output iterator)
<P>
An iterator's <i>distance type</i>, or <i>difference type</i> (the terms
are synonymous) is the type that is used to represent the distance
between two iterators. (See the discussion in the <A href="InputIterator.html">Input Iterator</A>
requirements.) The function <tt><A href="distance_type.html">distance_type</A></tt> returns this
information in the same form that <tt><A href="value_type.html">value_type</A></tt> does: its argument
is an iterator, and it returns the value <tt>(Distance*) 0</tt>, where
<tt>Distance</tt> is the iterator's distance type. Similarly,
<tt><A href="iterator_traits.html">iterator_traits</A><I>::difference_type</tt> is <tt>I</tt>'s distance type.
<P>
Just as with <tt><A href="value_type.html">value_type</A></tt>, the function <tt><A href="distance_type.html">distance_type</A></tt> need
not be defined for <A href="OutputIterator.html">Output Iterators</A>, and, if <tt>I</tt> is an
<A href="OutputIterator.html">Output Iterator</A>, <tt><A href="iterator_traits.html">iterator_traits</A><I>::difference_type</tt>
may be defined as <tt>void</tt>. An <A href="OutputIterator.html">Output Iterator</A>
need not have a distance type.
<P>
The functions <tt><A href="iterator_category.html">iterator_category</A></tt>, <tt><A href="value_type.html">value_type</A></tt>, and
<tt><A href="distance_type.html">distance_type</A></tt> must be provided for every type of iterator.
(Except, as noted above, that <tt><A href="value_type.html">value_type</A></tt> and <tt><A href="distance_type.html">distance_type</A></tt>
need not be provided for <A href="OutputIterator.html">Output Iterators</A>.) In principle, this is
simply a matter of overloading: anyone who defines a new iterator type
must define those three functions for it. In practice, there's a
slightly more convenient method. The STL defines five base classes,
<tt><A href="output_iterator.html">output_iterator</A></tt>, <tt><A href="input_iterator.html">input_iterator</A></tt>, <tt><A href="forward_iterator.html">forward_iterator</A></tt>,
<tt><A href="bidirectional_iterator.html">bidirectional_iterator</A></tt>, and <tt><A href="random_access_iterator.html">random_access_iterator</A></tt>. The
functions <tt><A href="iterator_category.html">iterator_category</A></tt>, <tt><A href="value_type.html">value_type</A></tt>, and
<tt><A href="distance_type.html">distance_type</A></tt> are defined for those base classes. The effect,
then, is that if you are defining a new type of iterator you can
simply derive it from one of those base classes, and the iterator tag
functions will automatically be defined correctly. These base classes
contain no member functions or member variables, so deriving from one
of them ought not to incur any overhead.
<P>
(Again, note that base classes are provided solely for the
convenience of people who define iterators. If you define a class
<tt>Iter</tt> that is a new kind of <A href="BidirectionalIterator.html">Bidirectional Iterator</A>, you do not
have to derive it from the base class <tt><A href="bidirectional_iterator.html">bidirectional_iterator</A></tt>. You
do, however, have to make sure that <tt><A href="iterator_category.html">iterator_category</A></tt>,
<tt><A href="value_type.html">value_type</A></tt>, and <tt><A href="distance_type.html">distance_type</A></tt> are defined correctly for
arguments of type <tt>Iter</tt>, and deriving <tt>Iter</tt> from
<tt><A href="bidirectional_iterator.html">bidirectional_iterator</A></tt> is usually the most convenient way to do
that.)
<h3>Examples</h3>
This example uses the <tt><A href="value_type.html">value_type</A></tt> iterator tag function in order
to declare a temporary variable of an iterator's value type. Note the
use of an auxiliary function, <tt>__iter_swap</tt>. This is a very common
idiom: most uses of iterator tags involve auxiliary functions.
<pre>
template <class <A href="ForwardIterator.html">ForwardIterator</A>1, class <A href="ForwardIterator.html">ForwardIterator</A>2, class ValueType>
inline void __iter_swap(ForwardIterator1 a, ForwardIterator2 b, ValueType*) {
ValueType tmp = *a;
*a = *b;
*b = tmp;
}
template <class <A href="ForwardIterator.html">ForwardIterator</A>1, class <A href="ForwardIterator.html">ForwardIterator</A>2>
inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) {
__iter_swap(a, b, <A href="value_type.html">value_type</A>(a));
}
</pre>
<P>
This example does exactly the same thing, using <tt><A href="iterator_traits.html">iterator_traits</A></tt>
instead. Note how much simpler it is: the auxiliary function is
no longer required.
<pre>
template <class <A href="ForwardIterator.html">ForwardIterator</A>1, class <A href="ForwardIterator.html">ForwardIterator</A>2>
inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) {
<A href="iterator_traits.html">iterator_traits</A><ForwardIterator1>::value_type tmp = *a;
*a = *b;
*b = tmp;
}
</pre>
<P>
This example uses the <tt><A href="iterator_category.html">iterator_category</A></tt>
iterator tag function: <tt><A href="reverse.html">reverse</A></tt> can be implemented for either
<A href="BidirectionalIterator.html">Bidirectional Iterator</A>s or for <A href="RandomAccessIterator.html">Random Access Iterators</A>,
but the algorithm for <A href="RandomAccessIterator.html">Random Access Iterators</A> is more efficient.
Consequently, <tt><A href="reverse.html">reverse</A></tt> is written to dispatch on the iterator
category. This dispatch takes place at compile time, and should not
incur any run-time penalty.
<pre>
template <class <A href="BidirectionalIterator.html">BidirectionalIterator</A>>
void __reverse(BidirectionalIterator first, BidirectionalIterator last,
<A href="bidirectional_iterator_tag.html">bidirectional_iterator_tag</A>) {
while (true)
if (first == last || first == --last)
return;
else
iter_swap(first++, last);
}
template <class <A href="RandomAccessIterator.html">RandomAccessIterator</A>>
void __reverse(RandomAccessIterator first, RandomAccessIterator last,
<A href="random_access_iterator_tag.html">random_access_iterator_tag</A>) {
while (first < last) iter_swap(first++, --last);
}
template <class <A href="BidirectionalIterator.html">BidirectionalIterator</A>>
inline void <A href="reverse.html">reverse</A>(BidirectionalIterator first, BidirectionalIterator last) {
__reverse(first, last, <A href="iterator_category.html">iterator_category</A>(first));
}
</pre>
<P>
In this case, <tt><A href="iterator_traits.html">iterator_traits</A></tt> would not be different in any
substantive way: it would still be necessary to use auxiliary
functions to dispatch on the iterator category. The only difference
is changing the top-level function to
<pre>
template <class <A href="BidirectionalIterator.html">BidirectionalIterator</A>>
inline void <A href="reverse.html">reverse</A>(BidirectionalIterator first, BidirectionalIterator last) {
__reverse(first, last,
<A href="iterator_traits.html">iterator_traits</A><first>::iterator_category());
}
</pre>
<h3>Concepts</h3>
<h3>Types</h3>
<UL>
<LI>
<tt><A href="output_iterator.html">output_iterator</A></tt>
<LI>
<tt><A href="input_iterator.html">input_iterator</A></tt>
<LI>
<tt><A href="forward_iterator.html">forward_iterator</A></tt>
<LI>
<tt><A href="bidirectional_iterator.html">bidirectional_iterator</A></tt>
<LI>
<tt><A href="random_access_iterator.html">random_access_iterator</A></tt>
</UL>
<UL>
<LI>
<tt><A href="output_iterator_tag.html">output_iterator_tag</A></tt>
<LI>
<tt><A href="input_iterator_tag.html">input_iterator_tag</A></tt>
<LI>
<tt><A href="forward_iterator_tag.html">forward_iterator_tag</A></tt>
<LI>
<tt><A href="bidirectional_iterator_tag.html">bidirectional_iterator_tag</A></tt>
<LI>
<tt><A href="random_access_iterator_tag.html">random_access_iterator_tag</A></tt>
</UL>
<UL>
<LI>
<tt><A href="iterator_traits.html">iterator_traits</A></tt>
</UL>
<h3>Functions</h3>
<UL>
<LI>
<tt><A href="iterator_category.html">iterator_category</A></tt>
<LI>
<tt><A href="value_type.html">value_type</A></tt>
<LI>
<tt><A href="distance_type.html">distance_type</A></tt>
</UL>
<h3>Notes</h3>
<P><A name="1">[1]</A>
<A href="OutputIterator.html">Output Iterators</A> have neither a distance type nor a value
type; in many ways, in fact, <A href="OutputIterator.html">Output Iterators</A> aren't really
iterators. Output iterators do not have a value type, because it is
impossible to obtain a value from an output iterator but only to write
a value through it. They do not have a distance type, similarly,
because it is impossible to find the distance from one output iterator
to another. Finding a distance requires a comparison for equality,
and output iterators do not support <tt>operator==</tt>.
<P><A name="2">[2]</A>
The <tt><A href="iterator_traits.html">iterator_traits</A></tt> class
relies on a C++ feature known as <i>partial specialization</i>. Many of
today's compilers don't implement the complete standard; in
particular, many compilers do not support partial specialization. If
your compiler does not support partial specialization, then you will
not be able to use <tt><A href="iterator_traits.html">iterator_traits</A></tt>, and you will have to
continue to use the older iterator tag functions.
<P><A name="3">[3]</A>
Note that <A href="trivial.html">Trivial Iterator</A> does not appear in this list.
The <A href="trivial.html">Trivial Iterator</A> concept is introduced solely for conceptual
clarity; the STL does not actually define any <A href="trivial.html">Trivial Iterator</A>
types, so there is no need for a <A href="trivial.html">Trivial Iterator</A> tag. There
is, in fact, a strong reason not to define one: the C++ type system
does not provide any way to distinguish between a pointer that is
being used as a trivial iterator (that is, a pointer to an object
that isn't part of an array) and a pointer that is being used as a
<A href="RandomAccessIterator.html">Random Access Iterator</A> into an array.
<h3>See also</h3>
<A href="InputIterator.html">Input Iterator</A>, <A href="OutputIterator.html">Output Iterator</A>, <A href="ForwardIterator.html">Forward Iterator</A>,
<A href="BidirectionalIterator.html">Bidirectional Iterator</A>, <A href="RandomAccessIterator.html">Random Access Iterator</A>,
<tt><A href="iterator_traits.html">iterator_traits</A></tt>, <A href="Iterators.html">Iterator Overview</A>
<!--start footer-->
<HR SIZE="6">
<A href="http://www.sgi.com/"><IMG SRC="surf.gif" HEIGHT="54" WIDTH="54"
ALT="[Silicon Surf]"></A>
<A HREF="index.html"><IMG SRC="stl_home.gif"
HEIGHT="54" WIDTH="54" ALT="[STL Home]"></A>
<BR>
<FONT SIZE="-2">
<A href="http://www.sgi.com/Misc/sgi_info.html" TARGET="_top">Copyright ©
1999 Silicon Graphics, Inc.</A> All Rights Reserved.</FONT>
<FONT SIZE="-3"><a href="http://www.sgi.com/Misc/external.list.html" TARGET="_top">TrademarkInformation</A>
</FONT>
<P>
</BODY>
</HTML>
|