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

📄 int_2158.htm

📁 ARM编辑、编译软件
💻 HTM
字号:
<HTML><HEAD><TITLE>Introduction to Iterators</TITLE></HEAD>
<BODY>
<A HREF="ug.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the user guide home page.</STRONG></P>
<P>&copy;Copyright 1996 Rogue Wave Software</P>
<H2>Introduction to Iterators</H2>
<A HREF="sidebar.htm#sidebar1"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Iterators</STRONG></A>

<P>Fundamental to the use of the container classes and the associated algorithms provided by the standard library is the concept of an <I>iterator</I>. Abstractly, an iterator is simply a pointer-like object used to cycle through all the elements stored in a container.  Because different algorithms need to traverse containers in a variety of fashions, there are different forms of iterator.  Each container class in the standard library can generate an iterator with functionality appropriate to the storage technique used in implementing the container.  It is the category of iterators required as arguments that chiefly distinguishes which algorithms in the standard library can be used with which container classes. </P>
<A HREF="sidebar.htm#sidebar2"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Range</STRONG></A>

<P>Just as pointers can be used in a variety of ways in traditional programming, iterators are also used for a number of different purposes.  An iterator can be used to denote a specific value, just as a pointer can be used to reference a specific memory location.  On the other hand, a <I>pair</I> of iterators can be used to describe a <I>range</I> of values, just as two pointers can be used to describe a contiguous region of memory.  In the case of iterators, however, the values being described are not necessarily physically in sequence, but are rather logically in sequence, because they are derived from the same container, and the second follows the first in the order in which the elements are maintained by the container.</P>
<P>Conventional pointers can sometimes be <I>null</I>, meaning they point at nothing.  Iterators, as well, can fail to denote any specific value.  Just as it is a logical error to dereference a null pointer, it is an error to dereference an iterator that is not denoting a value. </P>
<P>When two pointers that describe a region in memory are used in a C++ program, it is conventional that the ending pointer is <I>not</I> considered to be part of the region. For example, an array named <SAMP>x</SAMP> of length ten is sometimes described as extending from <SAMP>x</SAMP> to <SAMP>x+10</SAMP>, even though the element at <SAMP>x+10</SAMP> is not part of the array.  Instead, the pointer value <SAMP>x+10</SAMP> is the<I> past-the-end</I> value - the element that is the next value <I>after</I> the end of the range being described.  Iterators are used similarly to describe a range.  The second value is not considered to be part of the range being denoted.  Instead, the second value is a <I>past-the-end</I> element, describing the next value in sequence after the final value  of the range.  Sometimes, as with pointers to memory, this will be an actual value in the container.  Other times it may be a special value, specifically constructed for the purpose.  In either case, it is not proper to dereference an iterator that is being used to specify the end of a range.  </P>
<P>Just as with conventional pointers, the fundamental operation used to modify an iterator is the increment operator (operator <SAMP>++</SAMP>).  When the increment operator is applied to an iterator that denotes the final value in a sequence, it will be changed to the "past the end" value.  An iterator <SAMP>j</SAMP> is said to be <I>reachable</I> from an iterator <SAMP>i</SAMP> if, after a finite sequence of applications of the expression <SAMP>++i</SAMP>, the iterator <SAMP>i</SAMP> becomes equal to <SAMP>j</SAMP>.</P>
<A HREF="sidebar.htm#sidebar3"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Iterator Ranges</STRONG></A>

<P>Ranges can be used to describe the entire contents of a container, by constructing an iterator to the initial element and a special "ending" iterator.  Ranges can also be used to describe subsequences within a single container, by employing two iterators to specific values.  Whenever two iterators are used to describe a range it is assumed, but not verified, that the second iterator is reachable from the first.  Errors can occur if this expectation is not satisfied.</P>
<P>In the remainder of this section we will describe the different forms of iterators used by the standard library, as well as various other iterator-related functions.</P>
<HR>
<A HREF="ite_2271.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc.htm"><IMG SRC="images/toc.gif"></A> <A HREF="var_0565.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>

⌨️ 快捷键说明

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