⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 var_0565.htm

📁 C++标准库 C++标准库 C++标准库 C++标准库
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>2.2 Varieties of Iterators</TITLE></HEAD><BODY><A HREF="ug1.htm"><IMG SRC="images/banner.gif"></A><BR><A HREF="int_2158.htm"><IMG SRC="images/prev.gif"></A><A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="str_7181.htm"><IMG SRC="images/next.gif"></A><BR><STRONG>Click on the banner to return to the user guide home page.</STRONG><H2>2.2 Varieties of Iterators</H2><P>There are five basic forms of iterators used in the standard library:</P><CENTER><TABLE CELLSPACING=3 CELLPADDING=3><TR VALIGN=top><TD><B>input iterator</B> <BR></TD><TD>read only, forward moving <BR></TD></TR><TR VALIGN=top><TD><B>output iterator</B> <BR></TD><TD>write only, forward moving <BR></TD></TR><TR VALIGN=top><TD><B>forward iterator</B><BR></TD><TD>both read and write, forward moving <BR></TD></TR><TR VALIGN=top><TD><B>bidirectional iterator</B> <BR></TD><TD>read and write, forward and backward moving <BR></TD></TR><TR VALIGN=top><TD><B>random access iterator</B><BR></TD><TD>read and write, random access <BR></TD></TR></TABLE></CENTER><P>Iterator categories are hierarchical.  Forward iterators can be used wherever input or output iterators are required, bidirectional iterators can be used in place of forward iterators, and random access iterators can be used in situations requiring bidirectionality.  </P><A NAME="idx9"><!></A><P>A second characteristic of iterators is whether or not they can be used to modify the values held by their associated container.  A <I>constant iterator</I> is one that can be used for access only, and cannot be used for modification.  Output iterators are never constant, and input iterators always are.  Other iterators may or may not be constant, depending upon how they are created.  There are both constant and non-constant bidirectional iterators, both constant and non-constant random access iterators, and so on.</P><P>The following table summarizes specific ways that various categories of iterators are generated by the containers in the standard library.</P><CENTER><TABLE BORDER CELLSPACING=3 CELLPADDING=3><TR VALIGN=top><TD><I>Iterator Form</I><BR></TD><TD> <I>Produced By</I><BR></TD></TR><TR VALIGN=top><TD><B>input iterator</B> <BR></TD><TD> <SAMP>istream_iterator</SAMP><BR></TD></TR><TR VALIGN=top><TD><B>output iterator</B> <BR></TD><TD> <SAMP>ostream_iterator</SAMP><BR><SAMP>inserter</SAMP><BR><SAMP>front_inserter</SAMP><BR><SAMP>back_inserter</SAMP><BR></TD></TR><TR VALIGN=top><TD><B>bidirectional iterator</B> <BR></TD><A NAME="idx10"><!></A><TD><A HREF="../stdlibcr/lis_3222.htm"><B><I>list</I></B></A><BR><A HREF="../stdlibcr/set_1649.htm"><B><I>set </I></B></A>and<A HREF="../stdlibcr/mul_0958.htm"><B><I> multiset</I></B></A><BR><A HREF="../stdlibcr/map_8018.htm"><B><I>map </I></B></A>and<A HREF="../stdlibcr/mul_8396.htm"><B><I> multimap</I></B></A><BR></TD></TR><TR VALIGN=top><TD><B>random access iterator</B> <BR></TD><TD>ordinary pointers <BR><A HREF="../stdlibcr/vec_0251.htm"><B><I>vector</I></B></A><BR><A HREF="../stdlibcr/deq_4164.htm"><B><I>deque</I></B></A><BR></TD></TR></TABLE></CENTER><P>In the following sections we will describe the capabilities and construction of each form of iterator.</P><A NAME="2.2.1"><H3>2.2.1 Input Iterators</H3></A><A NAME="idx11"><!></A><P>Input iterators are the simplest form of iterator.  To understand their capabilities, consider an example program.  The <SAMP>find()</SAMP> generic algorithm (to be described in more detail in <A HREF="sea_9743.htm#13.3.1">Section 13.3.1</A>), performs a simple linear search, looking for a specific value being held within a container.  The contents of the container are described using two iterators, here called <SAMP>first</SAMP> and <SAMP>last</SAMP>. While <SAMP>first </SAMP>is not equal to <SAMP>last</SAMP> the element denoted by <SAMP>first</SAMP> is compared to the test value.  If equal, the iterator, which now denotes the located element, is returned.  If not equal, the <SAMP>first</SAMP> iterator is incremented, and the loop cycles once more.  If the entire region of memory is examined without finding the desired value, then the algorithm returns the end-of-range iterator.</P><PRE><SAMP>template &#60;class InputIterator, class T></SAMP><SAMP>InputIterator </SAMP><SAMP>   find (InputIterator first, InputIterator last, const T&#38; value)</SAMP><SAMP>{</SAMP><SAMP>   while (first != last &#38;&#38; *first != value) </SAMP><SAMP>      ++first;</SAMP><SAMP>   return first;</SAMP><SAMP>}</SAMP></PRE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -