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

📄 chxe.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
is the condition that's used to terminate the <font color="#008000">while</font> loop.</P>
<P><B>Cleaning Up the List</B></P>
<P>There's one other time when you need to iterate over a list. That's when the program is about to terminate and you need to 
delete all the objects pointed to by the pointers in the list. The List Demo application performs this task in the view class's destructor, as shown in Listing E.9.</P>
<P><B>Listing E.9&#151;</B><B><I>CMyListView</I></B><B> destructor</B></P>
<pre><font 
color="#008000">CMyListView::~CMyListView()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">    // Iterate over the list, deleting each node.</font></pre>
<pre><font color="#008000">    while (!list.IsEmpty())</font></pre>

<pre><font color="#008000">    {</font></pre>
<pre><font color="#008000">        CNode* pNode = (CNode*)list.RemoveHead();</font></pre>
<pre><font color="#008000">        delete pNode;</font></pre>
<pre><font color="#008000">    }</font></pre>
<pre><font 
color="#008000">}</font></pre>
<P>The destructor in Listing E.9 iterates over the list in a <font color="#008000">while</font> loop until the <font color="#008000">IsEmpty()</font> member function returns <font color="#008000">TRUE</font>. Inside the loop, 
the program removes the head node from the list (which makes the next node in the list the new head) and deletes the node from memory. When the list is empty, all the nodes that the program allocated have been deleted.</P>
<blockquote><p><img 
src="caution.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/caution.gif">
<P>Don't forget that you're responsible for deleting every node that you create with the <font color="#008000">new</font> operator. If you fail to delete nodes, you could cause a memory leak. In a small program like this, a few wasted 
bytes don&#146;t matter, but in a long-running program adding and deleting hundreds or thousands of list nodes, you could create serious errors in your program. It's always good programming practice to delete any objects you allocate in memory.</P>
<p><img 
src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P><A HREF="index24.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index24.htm" target="text">Chapter 24</A>, &#147;Improving your Application&#146;s Performance,&#148; discusses memory management and preventing memory leaks.</P>
<p><img 
src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<H3><B>The Map Classes</B></H3>
<P>You can use MFC's mapped collection classes for creating lookup tables. For example, you might want to convert digits into the words that represent the numbers. That is, you might want to 
use the digit 1 as a key in order to find the word <I>one.</I> A mapped collection is perfect for this sort of task. Thanks to the many MFC map classes, you can use various types of data for keys and values.</P>
<P>The MFC map classes are <font 
color="#008000">CMapPtrToPtr</font>, <font color="#008000">CMapPtrToWord</font>, <font color="#008000">CMapStringToOb</font>, <font color="#008000">CMapStringToPtr</font>, <font color="#008000">CMapStringToString</font>, <font 
color="#008000">CMapWordToOb</font>, and <font color="#008000">CMapWordToPtr</font>. The first data type in the name is the key, and the second is the value type. So, for example, <font color="#008000">CMapStringToOb</font> uses strings as keys and objects 
as values, whereas <font color="#008000">CMapStringToString</font>, which this section uses in its examples, uses strings as both keys and values. All the map classes are similar, and so have similar member functions, which are listed and described in 
Table E.3.</P>
<P><B>Table E.3&#151;Functions of the Map Classes</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Function</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>
<pre><font color="#008000">GetCount()</font></pre>
<TD>
<P>Gets the number of map 
elements.</P>
<TR>
<TD>
<pre><font color="#008000">GetNextAssoc()</font></pre>
<TD>
<P>Gets the next element when iterating over the map.</P>
<TR>
<TD>
<pre><font color="#008000">GetStartPosition()</font></pre>
<TD>
<P>Gets the first element's 
position.</P>
<TR>
<TD>
<pre><font color="#008000">IsEmpty()</font></pre>
<TD>
<P>Returns <font color="#008000">TRUE</font> if the map is empty and returns <font color="#008000">FALSE</font> otherwise.</P>
<TR>
<TD>
<pre><font 
color="#008000">Lookup()</font></pre>
<TD>
<P>Finds the value associated with a key.</P>
<TR>
<TD>
<pre><font color="#008000">RemoveAll()</font></pre>
<TD>
<P>Removes all the map's elements.</P>
<TR>
<TD>
<pre><font color="#008000">RemoveKey()</font></pre>

<TD>
<P>Removes an element from the map.</P>
<TR>
<TD>
<pre><font color="#008000">SetAt()</font></pre>
<TD>
<P>Adds a map element or replaces an element with a matching key.</P></TABLE>
<P><B>Introducing the Map Application</B></P>
<P>This section's 
example program, Map, displays the contents of a map and enables you to retrieve values from the map by giving the program the appropriate key. When you run the program, you see the window shown in Figure E.12.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">

<P>This application is on the CD in the RefE folder.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<A HREF="IIfig12.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig12.gif"><b>Fig. E.12</b></A>
<P><I>The Map application displays the contents of a map object.</I></P>
<P>The window displays the 
contents of the application's map object, in which digits are used as keys to access the words that represent the numbers. To retrieve a value from the map, click in the window. You then see the dialog box shown in Figure E.13. Type the digit that you want 
to use for a key and then click OK. The program finds the matching value in the map and displays it in another message box. For example, if you type 8 as the key, you see the message box shown in Figure E.14. If the key doesn't exist, the program's message 
box tells you so.</P>
<A HREF="IIfig13.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig13.gif"><b>Fig. E.13</b></A>
<P><I>The Get Map Value dialog box enables you to match a key with the key's </I><I>value in the map.</I></P>
<A HREF="IIfig14.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig14.gif"><b>Fig. E.14</b></A>
<P><I>This message 
box displays the requested map value.</I></P>
<P><B>Creating and Initializing the Map</B></P>
<P>The Map Demo application starts off with a ten-element map. The map object is declared as a data member of the view class, like this:</P>
<pre><font 
color="#008000">CMapStringToString map;</font></pre>
<P>This is an object of the <font color="#008000">CMapStringToString</font> class, which means that the map uses strings as keys and strings as values.</P>
<P>Declaring the map object doesn't, of course, 
fill it with values. You have to do that on your own, which the Map Demo application does in its view class constructor, shown in Listing E.10.</P>
<P><B>Listing E.10&#151;</B><B><I>CMapView</I></B><B> constructor</B></P>
<pre><font 
color="#008000">CMapView::CMapView()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">     map.SetAt(&quot;1&quot;, &quot;One&quot;);</font></pre>
<pre><font color="#008000">     map.SetAt(&quot;2&quot;, 
&quot;Two&quot;);</font></pre>
<pre><font color="#008000">     map.SetAt(&quot;3&quot;, &quot;Three&quot;);</font></pre>
<pre><font color="#008000">     map.SetAt(&quot;4&quot;, &quot;Four&quot;);</font></pre>
<pre><font color="#008000">     
map.SetAt(&quot;5&quot;, &quot;Five&quot;);</font></pre>
<pre><font color="#008000">     map.SetAt(&quot;6&quot;, &quot;Six&quot;);</font></pre>
<pre><font color="#008000">     map.SetAt(&quot;7&quot;, &quot;Seven&quot;);</font></pre>
<pre><font 
color="#008000">     map.SetAt(&quot;8&quot;, &quot;Eight&quot;);</font></pre>
<pre><font color="#008000">     map.SetAt(&quot;9&quot;, &quot;Nine&quot;);</font></pre>
<pre><font color="#008000">     map.SetAt(&quot;10&quot;, &quot;Ten&quot;);</font></pre>

<pre><font color="#008000">}</font></pre>
<P>The <font color="#008000">SetAt()</font> function takes as parameters the key and the value to associate with the key in the map. If the key already exists, the function replaces the value associated with the 
key with the new value given as the second argument.</P>
<P><B>Retrieving a Value from the Map</B></P>
<P>When you click in Map's window, the Get Map Value dialog box appears, so it&#146;s probably not surprising that the view class <font 
color="#008000">OnLButtonDown()</font> member function comes into play somewhere. Listing E.11 shows this function.</P>
<P><B>Listing E.11&#151;</B><B><I>CMapView::OnLButtonDown()</I></B></P>
<pre><font color="#008000">void CMapView::OnLButtonDown(UINT 
nFlags, CPoint point) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">// Initialize the dialog box.    </font></pre>
<pre><font color="#008000">    GetMapDlg dialog(this);</font></pre>
<pre><font color="#008000">    
dialog.m_key = &quot;&quot;;</font></pre>
<pre><font color="#008000">    // Display the dialog box.</font></pre>
<pre><font color="#008000">    int result = dialog.DoModal();</font></pre>
<pre><font color="#008000">    // If the user exits with the OK 
button...</font></pre>
<pre><font color="#008000">    if (result == IDOK)</font></pre>
<pre><font color="#008000">    {</font></pre>
<pre><font color="#008000">        // Look for the requested value.</font></pre>
<pre><font color="#008000">        CString 
value;</font></pre>
<pre><font color="#008000">        BOOL found = map.Lookup(dialog.m_key, value);</font></pre>
<pre><font color="#008000">        if (found)</font></pre>
<pre><font color="#008000">            MessageBox(value);</font></pre>
<pre><font 
color="#008000">        else</font></pre>
<pre><font color="#008000">            MessageBox(&quot;No matching value.&quot;);</font></pre>
<pre><font color="#008000">    }</font></pre>
<pre><font color="#008000">    CView::OnLButtonDown(nFlags, 
point);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>In <font color="#008000">OnLButtonDown()</font>, the program displays the dialog box in the usual way, checking to see whether the user exited the dialog box by clicking the OK button. If 
the user did, the program calls the map object's <font color="#008000">Lookup()</font> member function, using the key that the user entered into the dialog box as the first argument. The second argument is a reference to the string into which the function 
can store the value it retrieves from the map. If the key can't be found, the <font color="#008000">Lookup()</font> function returns <font color="#008000">FALSE</font>; otherwise, it returns <font color="#008000">TRUE</font>. The program uses this return 
value in order to determine whether it should display the string value retrieved from the map or a message box indicating an error.</P>
<P><B>Iterating Over the Map</B></P>
<P>In order to display the keys and values used in the map, the program must 
iterate over the map, moving from one entry to the next, retrieving and displaying the information for each map element. As with the array and list examples, the Map Demo application accomplishes this in its <font color="#008000">OnDraw()</font> function, 
which is shown in Listing E.12.</P>
<P><B>Listing E.12&#151;</B><B><I>CMapView::OnDraw()</I></B></P>
<pre><font color="#008000">void CMapView::OnDraw(CDC* pDC)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">    CMapDoc* 
pDoc = GetDocument();</font></pre>
<pre><font color="#008000">    ASSERT_VALID(pDoc);</font></pre>
<pre><font color="#008000">    TEXTMETRIC textMetric;</font></pre>
<pre><font color="#008000">    pDC-&gt;GetTextMetrics(&amp;textMetric);</font></pre>

<pre><font color="#008000">    int fontHeight = textMetric.tmHeight;</font></pre>
<pre><font color="#008000">    int displayPosition = 10;</font></pre>
<pre><font color="#008000">    POSITION pos = map.GetStartPosition();</font></pre>
<pre><font 
color="#008000">    CString key;</font></pre>
<pre><font color="#008000">    CString value;</font></pre>
<pre><font color="#008000">    while (pos != NULL)</font></pre>
<pre><font color="#008000">    {</font></pre>
<pre><font color="#008000">        
map.GetNextAssoc(pos, key, value);</font></pre>
<pre><font color="#008000">        CString str = &quot;Key '&quot; + key + </font></pre>
<pre><font color="#008000">            &quot;' is associated with the value '&quot; +</font></pre>
<pre><font 
color="#008000">            value + &quot;'&quot;;</font></pre>
<pre><font color="#008000">        pDC-&gt;TextOut(10, displayPosition, str);</font></pre>
<pre><font color="#008000">        displayPosition += fontHeight;</font></pre>
<pre><font 
color="#008000">    }</font></pre>
<pre><font color="#008000">}</font></pre>

⌨️ 快捷键说明

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