blob: 69308a19eb16148c2f8bf315e258aaa51bf10c3f [file] [log] [blame]
<HTML>
<HEAD>
<TITLE>Differences between Stream Iterators and Container Iterators</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="43-1.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="43-3.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library User's Guide</B></DIV>
<H2>43.2 Differences between Stream Iterators and Container Iterators</H2>
<A NAME="idx977"><!></A>
<P>In contrast to regular container iterators, stream iterators can provide only non-modifiable read-only access or write-only access to their elements. The <B><I><A HREF="../stdlibref/istream-iterator.html">istream_iterator</A></I></B> template is an InputIterator that can access but not modify elements. The <B><I><A HREF="../stdlibref/ostream-iterator.html">ostream_iterator</A></I></B> tempate, on the other hand, is an OutputIterator that provides write-only access to streams.</P>
<P>Each time the <B><I><A HREF="../stdlibref/istream-iterator.html">istream_iterator</A></I></B> object invokes <SAMP>operator++()</SAMP>, a new value from the stream is read and stored in value of type <SAMP>T,</SAMP> where <SAMP>T</SAMP> is the type of the element being written. The cached value, which by definition cannot be overwritten by assignment, is then available through the use of the dereferencing <SAMP>operator*()</SAMP>. This behavior of storing elements in the iterator is unique to <B><I>istream_iterator</I></B>s.</P>
<P>Also unlike container iterators, stream iterators can access elements only once, and only in the forward-moving direction, so that they can work only with one-pass algorithms. If the contents of the stream are to be read more than once, separate iterators must be created for each pass. Due to this unique nature of InputIterators it is inadvisable to use more than one <B><I><A HREF="../stdlibref/istream-iterator.html">istream_iterator</A></I></B> with the same stream object at the same time.</P>
<P>The <B><I><A HREF="../stdlibref/istream-iterator.html">istream_iterator</A></I></B> has a template parameter named <SAMP>Distance</SAMP>, with the default value of <SAMP>std::ptrdiff_t</SAMP> (which is defined in <SAMP>&lt;cstddef&gt;</SAMP>), although the <SAMP>Distance</SAMP> parameter is not used in the implementation. There is no distance type template argument for <B><I><A HREF="../stdlibref/ostream-iterator.html">ostream_iterator</A></I></B>, since pure OutputIterators do not have the notion of distance!</P>
<BR>
<HR>
<A HREF="43-1.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="43-3.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>