📄 iterator_tags.html
字号:
<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 TEXT="#000000" LINK="#006600" ALINK="#003300" VLINK="#7C7F87" BGCOLOR="#FFFFFF"><A HREF="/"><IMG SRC="/images/common/sgilogo_small.gif" ALT="SGI Logo" WIDTH="80" HEIGHT="72" BORDER="0"></A><P><!--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 isassociated with iterators. Specifically, an iterator type must, asdiscussed in the <A href="InputIterator.html">Input Iterator</A> requirements, have anassociated <i>distance type</i> and <i>value type</i>. <A href="#1">[1]</A> It is sometimesimportant for an algorithm parameterized by an iterator type to beable to determine the distance type and value type. Iterator tagsalso allow algorithms to determine an iterator's category, so thatthey 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 ofaccessing the type information associated with iterators: they weredefined in the original STL. The draft C++ standard, however, definesa 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 backwardscompatibility, but the older mechanism will eventually beremoved.<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 typeinformation, 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> areused to determine an iterator's value type, distance type, anditerator category.<P>An iterator's <i>category</i> is the most specific concept that it is amodel 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 fivecategory 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 ofthose concepts. <A href="#3">[3]</A> <P>The function <tt><A href="iterator_category.html">iterator_category</A></tt> takes a single argument, aniterator, and returns the tag corresponding to that iterator'scategory. That is, it returns a <tt><A href="random_access_iterator_tag.html">random_access_iterator_tag</A></tt> ifits argument is a pointer, a <tt><A href="bidirectional_iterator_tag.html">bidirectional_iterator_tag</A></tt> if itsargument 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> isan 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 returnedwhen 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 returnthe 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 beused 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> neednot 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 termsare synonymous) is the type that is used to represent the distancebetween 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 thisinformation in the same form that <tt><A href="value_type.html">value_type</A></tt> does: its argumentis 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> neednot 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 issimply a matter of overloading: anyone who defines a new iterator typemust define those three functions for it. In practice, there's aslightly 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>. Thefunctions <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 cansimply derive it from one of those base classes, and the iterator tagfunctions will automatically be defined correctly. These base classescontain no member functions or member variables, so deriving from oneof them ought not to incur any overhead. <P>(Again, note that base classes are provided solely for theconvenience 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 nothave to derive it from the base class <tt><A href="bidirectional_iterator.html">bidirectional_iterator</A></tt>. Youdo, 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 forarguments 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 dothat.)<h3>Examples</h3>This example uses the <tt><A href="value_type.html">value_type</A></tt> iterator tag function in orderto declare a temporary variable of an iterator's value type. Note theuse of an auxiliary function, <tt>__iter_swap</tt>. This is a very commonidiom: 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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -