📄 iterator_tags.html
字号:
__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 isno 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 iteratorcategory. This dispatch takes place at compile time, and should notincur 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 anysubstantive way: it would still be necessary to use auxiliary functions to dispatch on the iterator category. The only differenceis 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 valuetype; in many ways, in fact, <A href="OutputIterator.html">Output Iterators</A> aren't reallyiterators. Output iterators do not have a value type, because it isimpossible to obtain a value from an output iterator but only to writea value through it. They do not have a distance type, similarly,because it is impossible to find the distance from one output iteratorto 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> classrelies on a C++ feature known as <i>partial specialization</i>. Many oftoday's compilers don't implement the complete standard; inparticular, many compilers do not support partial specialization. Ifyour compiler does not support partial specialization, then you willnot 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 conceptualclarity; 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. Thereis, in fact, a strong reason not to define one: the C++ type systemdoes not provide any way to distinguish between a pointer that isbeing used as a trivial iterator (that is, a pointer to an objectthat 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 --><!-- Footer Begins --><STYLE TYPE="text/css"><!--TD.footer, TD.footer A{ font-family: Arial, helvetica, sans-serif; font-size: 8pt;}A.home {font-family: Arial, helvetica, sans-serif;}--></STYLE><P><A CLASS="home" HREF="index.html">STL Home</A><P><TABLE WIDTH="600" CELLPADDING="0" CELLPADDING="0" BORDER="0"> <TR> <TD ALIGN="RIGHT" CLASS="footer"><A HREF="/company_info/terms.html" TARGET="_top">terms of use</A> | <A HREF="/company_info/privacy.html" TARGET="_top">privacy policy</A></TD> <TD ALIGN="CENTER" CLASS="footer"> | </TD> <TD ALIGN="LEFT" CLASS="footer"><A HREF="/cgi-bin/feedback/" TARGET="_top">contact us</A></TD> </TR><TR> <TD ALIGN="RIGHT" CLASS="footer">Copyright © 1993-2003 Silicon Graphics, Inc. All rights reserved.</TD> <TD ALIGN="CENTER" CLASS="footer"> | </TD> <TD ALIGN="LEFT" CLASS="footer"><A HREF="/company_info/trademarks/" TARGET="_top">Trademark Information</A></TD> </TR></TABLE><!-- Footer Ends --><!-- end footer --><P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -