📄 page171.html
字号:
<HTML>
<HEAD>
<TITLE>Finding Items in a List</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="tex2html4027" HREF="page172.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page172.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="tex2html4025" HREF="page168.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page168.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="tex2html4019" HREF="page170.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page170.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="tex2html4029" 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="tex2html4030" 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="SECTION008113000000000000000">Finding Items in a List</A></H3>
<P>
Program <A HREF="page171.html#proglist2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page171.html#proglist2c"><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> defines two <tt>ListAsArray</tt> class
accessor functions which search for an object in the ordered list.
The <tt>IsMember</tt> function tests whether a particular object
instance is in the ordered list.
The <tt>Find</tt> function locates in the list
an object which <em>matches</em> its argument.
<P>
<P><A NAME="9606"> </A><A NAME="proglist2c"> </A> <IMG WIDTH=575 HEIGHT=295 ALIGN=BOTTOM ALT="program9447" SRC="img788.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img788.gif" ><BR>
<STRONG>Program:</STRONG> <tt>ListAsArray</tt> Class <tt>IsMember</tt> and <tt>Find</tt> Member Function Definitions<BR>
<P>
<P>
The <tt>IsMember</tt> function is a Boolean-valued function
which takes as its lone argument a <tt>const</tt> reference to an <tt>Object</tt>.
This function compares one-by-one the pointers contained in <tt>array</tt>
with the <em>address</em> of the argument.
Thus, this function tests whether <em>a particular object instance</em>
is contained in the ordered list.
In the worst case, the object sought is not in the list.
In this case, the running time of the function is <I>O</I>(<I>n</I>),
where <IMG WIDTH=72 HEIGHT=9 ALIGN=BOTTOM ALT="tex2html_wrap_inline61308" SRC="img709.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img709.gif" > is the number of items in the ordered list.
<P>
The <tt>Find</tt> function also does a search of the ordered list.
It also takes a single argument which is a <tt>const</tt> reference to
an <tt>Object</tt>.
However, find does not compare addresses.
Instead, it uses <tt>operator==</tt> to compare the items.
Thus, the <tt>Find</tt> function searches the list
for an object which compares equal to its argument.
The <tt>Find</tt> function returns a reference to the object found.
If no match is found,
it returns a reference to the <tt>NullObject</tt> instance.
The running time of this function depends on the time required
for the comparison operator, <IMG WIDTH=89 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline61648" SRC="img789.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img789.gif" >.
In the worst case, the object sought is not in the list.
In this case the running time is <IMG WIDTH=172 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline61650" SRC="img790.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img790.gif" >.
For simplicity, we will assume that the comparison
takes a constant amount of time.
Hence, the running time of the function is also <I>O</I>(<I>n</I>),
where <IMG WIDTH=72 HEIGHT=9 ALIGN=BOTTOM ALT="tex2html_wrap_inline61308" SRC="img709.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img709.gif" > is the number of items in the list.
<P>
It is important to understand the
subtle distinction between the search done by the
<tt>IsMember</tt> function and and that done by <tt>Find</tt>.
The <tt>IsMember</tt> function searches for a specific object instance
while <tt>Find</tt> simply looks for a matching object.
Consider the following:
<PRE>Object& object1 = *new Int (57);
Object& object2 = *new Int (57);
ListAsArray list (1);
list.Insert (object1);</PRE>
This code fragment creates two <tt>Int</tt> class object instances,
both of which have the value 57.
Only the first object, <tt>object1</tt>,
is inserted into the ordered list <tt>list</tt>.
Consequently, the function call
<PRE>list.IsMember (object1)</PRE>
returns <tt>true</tt>;
whereas the function call
<PRE>list.IsMember (object2)</PRE>
returns <tt>false</tt>.
<P>
On the other hand,
if a search is done using the <tt>Find</tt> function like this:
<PRE>Object& object3 = list.Find (object2);</PRE>
the search will be successful!
After the call, <tt>object3</tt> refers to <tt>object1</tt>.
<P>
<HR><A NAME="tex2html4027" HREF="page172.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page172.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="tex2html4025" HREF="page168.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page168.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="tex2html4019" HREF="page170.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page170.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="tex2html4029" 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="tex2html4030" 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -