page143.html
来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 89 行
HTML
89 行
<HTML>
<HEAD>
<TITLE>Iterator</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html3678" HREF="page144.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page144.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html3676" HREF="page138.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page138.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html3672" HREF="page142.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page142.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html3680" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html3681" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H3><A NAME="SECTION007125000000000000000">Iterator</A></H3>
<P>
The <tt>StackAsLinkedList::Iter</tt> class
is declared in Program <A HREF="page138.html#progstack3h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page138.html#progstack3h"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
Since the <tt>StackAsLinkedList::Iter</tt> class
is declared as a friend of the <tt>StackAsLinkedList</tt> class
the member functions of the former
can access the private member variables of the latter.
Consequently, the implementation of the iterator
depends on the implementation of the container.
<P>
<P><A NAME="6313"> </A><A NAME="progstack8c"> </A> <IMG WIDTH=575 HEIGHT=469 ALIGN=BOTTOM ALT="program6130" SRC="img721.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img721.gif" ><BR>
<STRONG>Program:</STRONG> <tt>StackAsLinkedList::Iter</tt> Class Member Function Definitions<BR>
<P>
<P>
<tt>StackAsLinkedList::Iter</tt> objects have two member variables,
<tt>stack</tt> and <tt>position</tt>.
The former is a reference to a <tt>StackAsLinkedList</tt> instance.
The latter is declared as a
<tt>ListElement<Object*> const*</tt>,
i.e, a pointer to a <tt>const</tt> element of a linked list
of pointers to <tt>Object</tt>s.
<P>
The <tt>StackAsLinkedList::Iter</tt> constructor simply
calls the <tt>Reset</tt> member function.
The <tt>Reset</tt> function makes
<tt>position</tt> point at the first element of the linked list
which represents the stack,
by calling the <tt>Head</tt> function of the <tt>LinkedList<T></tt> class.
Clearly the running time of the constructor is <I>O</I>(1).
<P>
The <tt>IsDone</tt> member function
simply tests for the null pointer.
If the <tt>position</tt> variable is zero,
<tt>IsDone</tt> returns <tt>true</tt>.
Again, the running time is clearly <I>O</I>(1).
<P>
The dereferencing operator, <tt>operator*</tt>,
does what its name says!
It returns a <tt>const</tt> reference to the object obtained
by dereferencing the object pointer contained in the list element
to which the <tt>position</tt> variable points.
This takes <I>O</I>(1) time.
Note that if <tt>pointer</tt> is zero,
a reference to the <tt>NullObject</tt> instance is returned.
<P>
Finally, the increment operator, <tt>operator++</tt>,
advances the <tt>position</tt> pointer to the next
element of the linked list.
This is done by calling the <tt>Next</tt> function
of the <tt>ListElement<T></tt> class.
This too is accomplished in constant time.
<P>
Because the interface of the <tt>StackAsLinkedList</tt> class
is exactly the same as that of the <tt>StackAsArray</tt> class,
<tt>StackAsLinkedList</tt> objects
can be used in exactly the same way as their array-based counterparts:
<PRE>StackAsLinkedList stack;
stack.Push (*new Int (3));
stack.Push (*new Int (1));
stack.Push (*new Int (4));
Iterator& i = stack.NewIterator ();
while (!i.IsDone ()) {
cout << *i << endl;
++i;
}
delete &i;</PRE>
This program fragment declares the variable <tt>stack</tt>,
pushes several values onto the stack,
and then uses an iterator to systematically print out all
of the elements contained in the stack.
<P>
<HR><A NAME="tex2html3678" HREF="page144.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page144.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html3676" HREF="page138.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page138.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html3672" HREF="page142.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page142.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html3680" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html3681" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright © 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?