?? apf.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Appendix F -- Useful Classes</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ape/ape.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../index.htm"><IMG
SRC="../button/contents.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Contents"
BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- F -</H1>
</CENTER>
<CENTER>
<H1>Useful Classes</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">The Array Classes</A>
<UL>
<LI><A HREF="#Heading2">Introducing the Array Application</A>
<LI><A HREF="#Heading3">Declaring and Initializing the Array</A>
<LI><A HREF="#Heading4">Adding Elements to the Array</A>
<LI><A HREF="#Heading5">Reading Through the Array</A>
<LI><A HREF="#Heading6">Removing Elements from the Array</A>
</UL>
<LI><A HREF="#Heading7">The List Classes</A>
<UL>
<LI><A HREF="#Heading8">Introducing the List Application</A>
<LI><A HREF="#Heading9">Declaring and Initializing the List</A>
<LI><A HREF="#Heading10">Adding a Node to the List</A>
<LI><A HREF="#Heading11">Deleting a Node from the List</A>
<LI><A HREF="#Heading12">Iterating Over the List</A>
<LI><A HREF="#Heading13">Cleaning Up the List</A>
</UL>
<LI><A HREF="#Heading14">The Map Classes</A>
<UL>
<LI><A HREF="#Heading15">Introducing the Map Application</A>
<LI><A HREF="#Heading16">Creating and Initializing the Map</A>
<LI><A HREF="#Heading17">Retrieving a Value from the Map</A>
<LI><A HREF="#Heading18">Iterating Over the Map</A>
</UL>
<LI><A HREF="#Heading19">Collection Class Templates</A>
<LI><A HREF="#Heading20">The String Class</A>
<LI><A HREF="#Heading21">The Time Classes</A>
<UL>
<LI><A HREF="#Heading22">Using a CTime Object</A>
<LI><A HREF="#Heading23">Using a CTimeSpan Object</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<P>MFC includes a lot more than classes for programming the Windows graphical user
interface. It also features many utility classes for handling such things as lists,
arrays, times and dates, and mapped collections. By using these classes, you gain
extra power over data in your programs and simplify many operations involved in using
complex data structures such as lists.</P>
<P>For example, because MFC's array classes can change their size dynamically, you
are relieved of creating oversized arrays in an attempt to ensure that the arrays
are large enough for the application. In this way, you save memory. You don't have
to worry about resizing the arrays yourself, and you avoid many of the subtle bugs
and memory leaks that occur from mistakes in array-resizing code. The other collection
classes provide many other similar conveniences.</P>
<P>
<H2><A NAME="Heading1"></A>The Array Classes</H2>
<P>MFC's array classes enable you to create and manipulate one-dimensional array
objects that can hold virtually any type of data. These array objects work much like
the standard arrays that you're familiar with using in your programs, except that
MFC can enlarge or shrink an array object dynamically at runtime. This means that
you don't have to be concerned with dimensioning your array just right when it's
declared. Because MFC's arrays can grow dynamically, you can forget about the memory
waste that often occurs with conventional arrays, which must be dimensioned to hold
the maximum number of elements needed in the program, whether or not you actually
use every element.</P>
<P>The array classes include CByteArray, CDWordArray, CObArray, CPtrArray, CUIntArray,
CWordArray, and CStringArray. As you can tell from the classnames, each class is
designed to hold a specific type of data. For example, the CUIntArray, which is used
in this section's examples, is an array class that can hold unsigned integers. The
CPtrArray class, on the other hand, represents an array of pointers to void, and
the CObArray class represents an array of objects. The array classes are all nearly
identical, differing only in the type of data that they store. When you've learned
to use one of the array classes, you've learned to use them all. Table F.1 lists
the member functions of the array classes and their descriptions.</P>
<P>
<H4>Table F.1  Member Functions of the Array Classes</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><B>Function</B></TD>
<TD ALIGN="LEFT"><B>Description</B></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Add()</TD>
<TD ALIGN="LEFT">Appends a value to the end of the array, increasing the size of the array, as needed.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">ElementAt()</TD>
<TD ALIGN="LEFT">Gets a reference to an array element's pointer.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">FreeExtra()</TD>
<TD ALIGN="LEFT">Releases unused array memory.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetAt()</TD>
<TD ALIGN="LEFT">Gets the value at the specified array index.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetSize()</TD>
<TD ALIGN="LEFT">Gets the number of elements in the array.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetUpperBound()</TD>
<TD ALIGN="LEFT">Gets the array's <I>upper bound,</I> which is the highest valid index at which a
value can be stored.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">InsertAt()</TD>
<TD ALIGN="LEFT">Inserts a value at the specified index, shifting existing elements upward, as necessary,
to accommodate the insert.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">RemoveAll()</TD>
<TD ALIGN="LEFT">Removes all the array's elements.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">RemoveAt()</TD>
<TD ALIGN="LEFT">Removes the value at the specified index.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">SetAt()</TD>
<TD ALIGN="LEFT">Places a value at the specified index. Because this function will not increase the
array's size, the index must be currently valid.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">SetAtGrow()</TD>
<TD ALIGN="LEFT">Places a value at the specified index, increasing the array's size, as needed.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">SetSize()</TD>
<TD ALIGN="LEFT">Sets the array's initial size and the amount by which it grows when needed. By allocating
more than one element's worth of space at a time, you save time but might waste memory.</TD>
</TR>
</TABLE>
<BLOCKQUOTE>
<P>
<HR>
<B>Array Templates</B></P>
<P>Because the only difference between all these array classes is the type of data
they hold, they seem like an obvious use for templates. In fact, they predate the
implementation of templates in Visual C++. There is a vector template in the Standard
Template Library, discussed in Chapter 26, "Exceptions and Templates,"
which holds simple lists of any single data type. Many developers find the MFC array
classes much easier to use than templates. There are also MFC collection templates,
discussed later in this chapter.
<HR>
</BLOCKQUOTE>
<H3><A NAME="Heading2"></A>Introducing the Array Application</H3>
<P>To illustrate how the array classes work, this chapter includes the Array application.
When you run the program, you see the window shown in Figure F.1. The window displays
the array's current contents. Because the application's array object (which is an
instance of CUIntArray) starts off with 10 elements, the values for these elements
(indexed as 0 through 9) are displayed onscreen. The application enables you to change,
add, or delete elements in the array and see the results.</P>
<P><A HREF="javascript:popUp('xfuvc01.gif')"><B>FIG. F.1</B></A><B> </B><I>The Array
application enables you to experiment with MFC's array classes.</I></P>
<P>You can add an element to the array in several ways. To see these choices, click
in the application's window. The dialog box shown in Figure F.2 appears. Type an
array index in the Index box and the new value in the Value box. Then select whether
you want to set, insert, or add the element. When you choose Set, the value of the
element you specify in the Index field is changed to the value in the Value field.
The Insert operation creates a new array element at the location specified by the
index, pushing succeeding elements forward. Finally, the Add operation tacks the
new element on the end of the array. In this case, the program ignores the Index
field of the dialog box.</P>
<P><A HREF="javascript:popUp('xfuvc02.gif')"><B>FIG. F.2</B></A><B> </B><I>The Add
to Array dialog box enables you to add elements to the array.</I></P>
<P>Suppose, for example, that you enter <B>3</B> in the dialog box's Index field
and <B>15</B> in the Value field, leaving the Set radio button selected. Figure F.3
shows the result: The program has placed the value 15 in element 3 of the array,
overwriting the previous value. Now type <B>2</B> in Index, <B>25</B> in Value, select
the Insert radio button, and click OK. Figure F.4 shows the result: The program stuffs
a new element in the array, shoving the other elements forward.</P>
<P><A HREF="javascript:popUp('xfuvc03.gif')"><B>FIG. F.3</B></A><B> </B><I>The value
15 has been placed in array element 3.</I></P>
<P>An interesting thing to try--something that really shows how dynamic MFC's arrays
are--is to set an array element beyond the end of the array. For example, given the
program's state shown in Figure F.4, if you type <B>20 </B>in Index and <B>45</B>
in Value and then choose the Set radio button, you get the results shown in Figure
F.5. Because there was no element 20, the array class created the new elements that
it needed to get to 20. You don't need to keep track of how many elements are in
the array. Try that with an old-fashioned array.</P>
<P><A HREF="javascript:popUp('xfuvc04.gif')"><B>FIG. F.4</B></A><B> </B><I>The screen
now shows the new array element, giving 11 elements in all.</I></P>
<P><A HREF="javascript:popUp('xfuvc05.gif')"><B>FIG. F.5</B></A><B> </B><I>The array
class has added the elements needed to set element 20.</I></P>
<P>Besides adding new elements to the array, you can also delete elements in one
of two ways. To do this, first right-click in the window. When you do, you see the
dialog box shown in Figure F.6. If you type an index in the Remove field and then
click OK, the program deletes the selected element from the array. This has the opposite
effect of the Insert command because the Remove command shortens the array, rather
than lengthen it. If you want, you can select the Remove All option in the dialog
box. Then the program deletes all elements from the array, leaving it empty.</P>
<P><A HREF="javascript:popUp('xfuvc06.gif')"><B>FIG. F.6</B></A><B> </B><I>The Remove
From Array dialog box enables you to delete elements from the array.</I></P>
<P>
<H3><A NAME="Heading3"></A>Declaring and Initializing the Array</H3>
<P>Now you'd probably like to see how all this array trickery works. It's really
pretty simple. First, the program declares the array object as a data member of the
view class, like this:</P>
<P>
<PRE>CUIntArray array;
</PRE>
<P>Then, in the view class's constructor, the program initializes the array to 10
elements:</P>
<P>
<PRE>array.SetSize(10, 5);
</PRE>
<P>The SetSize() function takes as parameters the number of elements to give the
array initially and the number of elements by which the array should grow whenever
it needs to. You don't need to call SetSize() to use the array class. If you don't,
MFC adds elements to the array one at a time, as needed, which can be slow. Unless
you're doing some heavy processing, though, you're not likely to notice any difference
in speed. If your application doesn't often add elements to its arrays and you are
concerned about memory consumption, don't use SetSize(). If your application repeatedly
adds elements and you have lots of memory available, using SetSize() to arrange for
many elements to be allocated at once will reduce the number of allocations performed,
giving you a faster application.</P>
<P>
<H3><A NAME="Heading4"></A>Adding Elements to the Array</H3>
<P>After setting the array size, the program waits for the user to click the left
or right mouse buttons in the window. When the user does, the program springs into
action, displaying the appropriate dialog box and processing the values entered in
the dialog box. Listing F.1 shows the Array application's OnLButtonDown() function,
which handles the left mouse button clicks.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>TIP:</STRONG> Chapter 3, "Messages and Commands," shows you how
to catch mouse clicks and arrange for a message handler such as OnLButtonDown() to
be called.
<HR>
</BLOCKQUOTE>
<H4>Listing F.1  CArrayView::OnLButtonDown()</H4>
<PRE>void CArrayView::OnLButtonDown(UINT nFlags, CPoint point)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -