📄 iterator_tags.html
字号:
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" tppabs="http://www.sgi.com/Technology/STL/ForwardIterator.shtml">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" tppabs="http://www.sgi.com/Technology/STL/RandomAccessIterator.shtml">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" tppabs="http://www.sgi.com/Technology/STL/OutputIterator.shtml">Output Iterator</A>, <A href="ForwardIterator.html" tppabs="http://www.sgi.com/Technology/STL/ForwardIterator.shtml">Forward Iterator</A>,
<A href="BidirectionalIterator.html">Bidirectional Iterator</A>, <A href="RandomAccessIterator.html" tppabs="http://www.sgi.com/Technology/STL/RandomAccessIterator.shtml">Random Access Iterator</A>,
<tt><A href="iterator_traits.html">iterator_traits</A></tt>, <A href="Iterators.html" tppabs="http://www.sgi.com/Technology/STL/Iterators.shtml">Iterator Overview</A>
<HR SIZE="6"> <FONT SIZE="-2"> Copyright © 1996 Silicon Graphics, Inc.
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="index.html" >
STL</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© Copyright 1997-1998 CodeGuru</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact : <A HREF="mailto:webmaster@codeguru.com">webmaster@codeguru.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
<SCRIPT LANGUAGE="JavaScript" ><!--
var adurl = "/cgi-bin/doubleclick.cgi?";
if( self.adcategory )
adurl += adcategory;
else
adurl += "mfc";
if( self.parent.norefreshad )
parent.norefreshad = false;
else if( validframes )
parent.frames['ad'].location = adurl;
if( !validframes && nfrm == -1)
{
var dclkPage = "www.codeguru.com/";
if( self.adcategory )
dclkPage += adcategory;
else
dclkPage += "mfc";
// var random = Math.random();
document.write('<nolayer><center>');
document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
+ random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
+ 'frameborder=0 scrolling=no bordercolor="#000000">');
document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
+ random + '">');
document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
+ random + '" height=60 width=468>' + '</a>');
document.write('</iframe>');
document.write('</center></nolayer>');
document.write('<layer src="http://ad.doubleclick.net/adl/' + dclkPage +
';ord=' + random + '"></layer>');
document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}
// -->
</SCRIPT>
<!-- SCRIPT LANGUAGE="JavaScript" SRC="/global/fscript.js">
//
</SCRIPT -->
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupaitFCY34AAHUFAmY">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupaitFCY34AAHUFAmY"></a>
</p>
</noscript>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -