﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-★Snowhill ★-文章分类-data structure</title><link>http://www.cppblog.com/snowhill/category/3193.html</link><description>&lt;font color=#5555aa &gt;&lt;b&gt; 颠沛必于是，造次必于是！&lt;/b&gt;&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Sun, 31 Jan 2010 18:22:08 GMT</lastBuildDate><pubDate>Sun, 31 Jan 2010 18:22:08 GMT</pubDate><ttl>60</ttl><item><title>VC6.0下ADO編程</title><link>http://www.cppblog.com/snowhill/articles/106728.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Fri, 29 Jan 2010 07:39:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/articles/106728.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/106728.html</wfw:comment><comments>http://www.cppblog.com/snowhill/articles/106728.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/106728.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/106728.html</trackback:ping><description><![CDATA[<table style="WIDTH: 1023px; HEIGHT: 62px" cellSpacing=0 cellPadding=0 width=1023 border=0>
    <tbody>
        <tr>
            <td colSpan=4 height=5></td>
        </tr>
        <tr>
            <td><font color=#ffffff>?</font></td>
            <td colSpan=3>
            <pre>使用ADO前必须在工程的stdafx.h文件最后用直接引入符号＃import引入ADO库文件，以使编译器能正确编译。代码如下：
            #import "C:\Program Files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
            </pre>
            </td>
        </tr>
    </tbody>
</table>
BOOL CAdoDlg::OnInitDialog()<br>{<br>&nbsp;CDialog::OnInitDialog();<br>&nbsp;m_list.InsertColumn(0,"spbh",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(1,"spmch",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(2,"spid",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(3,"jlgg",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(4,"shpchd",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(5,"shengccj",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(6,"dw",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(7,"shpgg",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(8,"denglrq",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(9,"beactive",LVCFMT_LEFT,50,-1);<br>&nbsp;m_list.InsertColumn(10,"tongym",LVCFMT_LEFT,50,-1);<br>&nbsp;&nbsp;&nbsp; ::CoInitialize(NULL);<br>&nbsp;//ADO 对象的实例化必须在COM环境初始化之后<br>&nbsp;m_pConn.CreateInstance(_uuidof(Connection));<br>&nbsp;m_pRs.CreateInstance(_uuidof(Recordset));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<br>&nbsp;m_pConn-&gt;Open("DSN=ccerp_jt","x","x",adConnectUnspecified); //open database connection　dsn,username,password,openmode&nbsp;:adConnectUnspcified,adAsynConnect(异步)<br>&nbsp;return TRUE;<br>｝ 
<img src ="http://www.cppblog.com/snowhill/aggbug/106728.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2010-01-29 15:39 <a href="http://www.cppblog.com/snowhill/articles/106728.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>有向图的邻接表遍历</title><link>http://www.cppblog.com/snowhill/articles/16235.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Sun, 10 Dec 2006 11:01:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/articles/16235.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/16235.html</wfw:comment><comments>http://www.cppblog.com/snowhill/articles/16235.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/16235.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/16235.html</trackback:ping><description><![CDATA[
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #008080">  1</span> <span style="COLOR: #008000">/*</span><span style="COLOR: #008000"> DATE:2006-12-10  BY:snowhill </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">  2</span> <span style="COLOR: #000000"> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">   有向图的邻接表 表示 </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">  3</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080">  4</span> <span style="COLOR: #000000">  #include   </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">   <br /></span><span style="COLOR: #008080">  5</span> <span style="COLOR: #000000">  #include   </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdlib.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">   <br /></span><span style="COLOR: #008080">  6</span> <span style="COLOR: #000000">  typedef  </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">  VertexType ;          </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">顶点数据类型  </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">  7</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">   MAX_VERTEX_NUM   20         </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">最大顶点数   </span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">  8</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">   MAX_EDGE_NUM   40          </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">最大边数   </span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">  9</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">   visited[MAX_VERTEX_NUM];       </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">访问标志数组</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 10</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">      <br /></span><span style="COLOR: #008080"> 11</span> <span style="COLOR: #000000">  </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">结点类型</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">  <br /></span><span style="COLOR: #008080"> 12</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">   ArcNode   <br /></span><span style="COLOR: #008080"> 13</span> <span style="COLOR: #000000">  {     <br /></span><span style="COLOR: #008080"> 14</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">   adjvex;           </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">该弧所在的顶点位置</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 15</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">      ArcNode   </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">nextarc;       </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">指向下一条弧</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 16</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">  };   <br /></span><span style="COLOR: #008080"> 17</span> <span style="COLOR: #000000">  </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"> 表头结点定义</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 18</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">   VNode   <br /></span><span style="COLOR: #008080"> 19</span> <span style="COLOR: #000000">  {     <br /></span><span style="COLOR: #008080"> 20</span> <span style="COLOR: #000000">      VertexType   data;           </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">顶点信息</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 21</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">      ArcNode   </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">firstarc;         </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">指向第一条弧 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 22</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">  };<br /></span><span style="COLOR: #008080"> 23</span> <span style="COLOR: #000000">  typedef VNode AdjList[MAX_VERTEX_NUM];   <br /></span><span style="COLOR: #008080"> 24</span> <span style="COLOR: #000000">  <br /></span><span style="COLOR: #008080"> 25</span> <span style="COLOR: #000000">  </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">图定义</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br /></span><span style="COLOR: #008080"> 26</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"> ALGraph    <br /></span><span style="COLOR: #008080"> 27</span> <span style="COLOR: #000000">  {     <br /></span><span style="COLOR: #008080"> 28</span> <span style="COLOR: #000000">      AdjList  vertices;   <br /></span><span style="COLOR: #008080"> 29</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">   vexnum,arcnum;   <br /></span><span style="COLOR: #008080"> 30</span> <span style="COLOR: #000000">  }; <br /></span><span style="COLOR: #008080"> 31</span> <span style="COLOR: #000000">  ALGraph  G;<br /></span><span style="COLOR: #008080"> 32</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080"> 33</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">   CreateDG(ALGraph   </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">G)   <br /></span><span style="COLOR: #008080"> 34</span> <span style="COLOR: #000000">  {   <br /></span><span style="COLOR: #008080"> 35</span> <span style="COLOR: #000000">   </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">   i,j,k;   <br /></span><span style="COLOR: #008080"> 36</span> <span style="COLOR: #000000">   ArcNode   </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p;   <br /></span><span style="COLOR: #008080"> 37</span> <span style="COLOR: #000000">   cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">创建一个有向图:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;   <br /></span><span style="COLOR: #008080"> 38</span> <span style="COLOR: #000000">   cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">顶点数:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;   cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">G.vexnum;   cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;   <br /></span><span style="COLOR: #008080"> 39</span> <span style="COLOR: #000000">   cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">边数:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;     cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">G.arcnum;   cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl; <br /></span><span style="COLOR: #008080"> 40</span> <span style="COLOR: #000000">     </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">        初始化        </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">  <br /></span><span style="COLOR: #008080"> 41</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">G.vexnum;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)   <br /></span><span style="COLOR: #008080"> 42</span> <span style="COLOR: #000000">         {   <br /></span><span style="COLOR: #008080"> 43</span> <span style="COLOR: #000000">         G.vertices[i].data</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;   <br /></span><span style="COLOR: #008080"> 44</span> <span style="COLOR: #000000">         G.vertices[i].firstarc</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;   <br /></span><span style="COLOR: #008080"> 45</span> <span style="COLOR: #000000">         } <br /></span><span style="COLOR: #008080"> 46</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080"> 47</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">G.arcnum;k</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)   <br /></span><span style="COLOR: #008080"> 48</span> <span style="COLOR: #000000">     {   <br /></span><span style="COLOR: #008080"> 49</span> <span style="COLOR: #000000">         cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">请输入第</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">k</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">条边:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;   <br /></span><span style="COLOR: #008080"> 50</span> <span style="COLOR: #000000">         cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">j;   <br /></span><span style="COLOR: #008080"> 51</span> <span style="COLOR: #000000">         p</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> ArcNode;   <br /></span><span style="COLOR: #008080"> 52</span> <span style="COLOR: #000000">         p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">adjvex</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j;   <br /></span><span style="COLOR: #008080"> 53</span> <span style="COLOR: #000000">         p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">nextarc</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">G.vertices[i].firstarc;   <br /></span><span style="COLOR: #008080"> 54</span> <span style="COLOR: #000000">         G.vertices[i].firstarc</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">p;   <br /></span><span style="COLOR: #008080"> 55</span> <span style="COLOR: #000000">     }   <br /></span><span style="COLOR: #008080"> 56</span> <span style="COLOR: #000000">  }   <br /></span><span style="COLOR: #008080"> 57</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080"> 58</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">   Disp(ALGraph G)   <br /></span><span style="COLOR: #008080"> 59</span> <span style="COLOR: #000000">  {   <br /></span><span style="COLOR: #008080"> 60</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">   i;   <br /></span><span style="COLOR: #008080"> 61</span> <span style="COLOR: #000000">      ArcNode   </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p;   <br /></span><span style="COLOR: #008080"> 62</span> <span style="COLOR: #000000">      cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">输出图为:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;   <br /></span><span style="COLOR: #008080"> 63</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">G.vexnum;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)   <br /></span><span style="COLOR: #008080"> 64</span> <span style="COLOR: #000000">          {   <br /></span><span style="COLOR: #008080"> 65</span> <span style="COLOR: #000000">         p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">G.vertices[i].firstarc;   <br /></span><span style="COLOR: #008080"> 66</span> <span style="COLOR: #000000">         </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(p</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">NULL)   <br /></span><span style="COLOR: #008080"> 67</span> <span style="COLOR: #000000">             {   <br /></span><span style="COLOR: #008080"> 68</span> <span style="COLOR: #000000">                 cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">adjvex</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;   <br /></span><span style="COLOR: #008080"> 69</span> <span style="COLOR: #000000">                 p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">nextarc;   <br /></span><span style="COLOR: #008080"> 70</span> <span style="COLOR: #000000">               } <br /></span><span style="COLOR: #008080"> 71</span> <span style="COLOR: #000000">        cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;   <br /></span><span style="COLOR: #008080"> 72</span> <span style="COLOR: #000000">          }   <br /></span><span style="COLOR: #008080"> 73</span> <span style="COLOR: #000000">          cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">*******************************</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br /></span><span style="COLOR: #008080"> 74</span> <span style="COLOR: #000000">  }   <br /></span><span style="COLOR: #008080"> 75</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080"> 76</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">   dfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> v)   </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">深度优先遍历   </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 77</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">  {   <br /></span><span style="COLOR: #008080"> 78</span> <span style="COLOR: #000000">  ArcNode </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> ArcNode; <br /></span><span style="COLOR: #008080"> 79</span> <span style="COLOR: #000000">  cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">G.vertices[v].data</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">＊＊</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;   <br /></span><span style="COLOR: #008080"> 80</span> <span style="COLOR: #000000">  visited[v]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;   <br /></span><span style="COLOR: #008080"> 81</span> <span style="COLOR: #000000">  p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">G.vertices[v].firstarc;   <br /></span><span style="COLOR: #008080"> 82</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(p</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">NULL)   <br /></span><span style="COLOR: #008080"> 83</span> <span style="COLOR: #000000">     { <br /></span><span style="COLOR: #008080"> 84</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">visited[p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">adjvex])   <br /></span><span style="COLOR: #008080"> 85</span> <span style="COLOR: #000000">         dfs(p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">adjvex);   <br /></span><span style="COLOR: #008080"> 86</span> <span style="COLOR: #000000">     p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">nextarc;   <br /></span><span style="COLOR: #008080"> 87</span> <span style="COLOR: #000000">     }   <br /></span><span style="COLOR: #008080"> 88</span> <span style="COLOR: #000000">  }<br /></span><span style="COLOR: #008080"> 89</span> <span style="COLOR: #000000">   <br /></span><span style="COLOR: #008080"> 90</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">   main()   <br /></span><span style="COLOR: #008080"> 91</span> <span style="COLOR: #000000">  {     <br /></span><span style="COLOR: #008080"> 92</span> <span style="COLOR: #000000">   <br /></span><span style="COLOR: #008080"> 93</span> <span style="COLOR: #000000">     </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> visited[</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">];<br /></span><span style="COLOR: #008080"> 94</span> <span style="COLOR: #000000">     </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> v;<br /></span><span style="COLOR: #008080"> 95</span> <span style="COLOR: #000000">     cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">/*</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">G=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;&amp;</span><span style="COLOR: #000000">G</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">*/</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;   <br /></span><span style="COLOR: #008080"> 96</span> <span style="COLOR: #000000">     CreateDG(G);   <br /></span><span style="COLOR: #008080"> 97</span> <span style="COLOR: #000000">     Disp(G);<br /></span><span style="COLOR: #008080"> 98</span> <span style="COLOR: #000000">     </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">G.vexnum;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080"> 99</span> <span style="COLOR: #000000">     visited[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">100</span> <span style="COLOR: #000000">     cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">请输入深度优先搜索的顶点:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">101</span> <span style="COLOR: #000000">     cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">v;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br /></span><span style="COLOR: #008080">102</span> <span style="COLOR: #000000">     cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">深度优先序列:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">; <br /></span><span style="COLOR: #008080">103</span> <span style="COLOR: #000000">     dfs(v);   <br /></span><span style="COLOR: #008080">104</span> <span style="COLOR: #000000">     cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;   <br /></span><span style="COLOR: #008080">105</span> <span style="COLOR: #000000">  } </span></div>
<img src ="http://www.cppblog.com/snowhill/aggbug/16235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-10 19:01 <a href="http://www.cppblog.com/snowhill/articles/16235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>无向图的邻接表</title><link>http://www.cppblog.com/snowhill/articles/16234.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Sun, 10 Dec 2006 10:55:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/articles/16234.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/16234.html</wfw:comment><comments>http://www.cppblog.com/snowhill/articles/16234.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/16234.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/16234.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Arial">
						<font style="BACKGROUND-COLOR: #f5f5dc">
								<font color="#009966">//DATE:2006-12-10    BY:snowhill<br />/*   1---2   以邻接表存放应为: 1-&gt;5-&gt;3-&gt;2<br />       | \ / |                      2-&gt;4-&gt;3-&gt;1<br />       | 3  |                       3-&gt;5-&gt;4-&gt;2-&gt;1<br />       |/ \  |                       4-&gt;5-&gt;3-&gt;2<br />      5---4                      5-&gt;4-&gt;3-&gt;1<br /> */<br />/*  图的邻接表  以a[1]为起始地起址存放 */</font>
								<br />#include "iostream.h"<br />const int Max_vertex=5;<br />const int Max_Edge=8;<br />int visited[Max_vertex+1]; <font color="#009933">//访问标志数组</font><br /></font>
				</font>
				<font face="Arial">
						<font style="BACKGROUND-COLOR: #f5f5dc">
								<strong>struct ArcNode<br /></strong>{<br /> int adjvex;<br /> ArcNode *nextarc;   </font>
				</font>
				<font face="Arial">
						<font style="BACKGROUND-COLOR: #f5f5dc">
								<font color="#009966">//指向下一条弧<br /></font>};<br /></font>
				</font>
				<font face="Arial">
						<font style="BACKGROUND-COLOR: #f5f5dc">
								<strong>struct Vnode<br /></strong>{<br /> int v;     //顶点信息<br /> ArcNode *next;<br />}<strong>a[Max_vertex+1];</strong></font>
				</font>
		</p>
		<p>
				<font style="BACKGROUND-COLOR: #f5f5dc" face="Arial">
						<font color="#0000ff">/* 无向图的建立 */<br /></font>void creategraph()<br />{<br /> int i,j,k;<br /> ArcNode *s;<br /> //初始化<br /> for(i=1;i&lt;=Max_vertex;i++)<br /> {<br />  a[i].v=i;<br />  a[i].next=NULL;<br /> }</font>
		</p>
		<p>
				<font style="BACKGROUND-COLOR: #f5f5dc" face="Arial"> /*以头插法建立 */ </font>
		</p>
		<p>
				<font style="BACKGROUND-COLOR: #f5f5dc" face="Arial">  for(k=1;k&lt;=Max_Edge;k++)<br /> {<br />  cout&lt;&lt;"请输入第"&lt;&lt;k&lt;&lt;"条边:";<br />  cin&gt;&gt;i&gt;&gt;j;<br />  if(i&gt;9||i&lt;0||j&lt;0||j&gt;9)<br />    {<br />     cout&lt;&lt;"输入错误!!\n"&lt;&lt;endl;<br />     break;<br />      }<br />  else{  <br />   cout&lt;&lt;endl;<br />   s=new ArcNode;<br />   s-&gt;adjvex=j;<br />   s-&gt;nextarc=a[i].next;<br />   a[i].next=s;<br />   s=new ArcNode;<br />   s-&gt;adjvex=i;<br />   s-&gt;nextarc=a[j].next;<br />   a[j].next=s;<br />   }<br /> }<br />}</font>
		</p>
		<p>
				<font face="Arial">
						<font style="BACKGROUND-COLOR: #f5f5dc">
								<font color="#0000ff">/* 深度优先搜索 */</font>
								<br />void <strong>dfs</strong>(int i)<br />{<br />  ArcNode *p;<br />  cout&lt;&lt;a[i].v&lt;&lt;" ";<br />  visited[i]=1;<br />  p=a[i].next;<br />  while(p!=NULL)<br />  {<br />   if(!visited[p-&gt;adjvex])<br />   dfs(p-&gt;adjvex);<br />   p=p-&gt;nextarc;<br />  }<br />}<br /><font color="#0000ff"><strong>void display()</strong></font><br />{ <br />  ArcNode *p;<br />  cout&lt;&lt;"你建立的图为:"&lt;&lt;endl;<br />  for(int i=1;i&lt;=Max_vertex;i++)<br />  {<br />  p=a[i].next;<br />  cout&lt;&lt;a[i].v&lt;&lt;"-&gt;";<br />  while(p-&gt;nextarc!=NULL)<br />   {<br />    cout&lt;&lt;p-&gt;adjvex&lt;&lt;"-&gt;";<br />    p=p-&gt;nextarc;<br />   }<br />  cout&lt;&lt;p-&gt;adjvex&lt;&lt;endl;<br />  }<br />}<br /><strong>void main</strong>()<br />{  <br /> cout&lt;&lt;"/******\t本算法以关插法建立无向图的邻接表为例!\t******/"&lt;&lt;endl;<br />    char yn='y';int k;<br /> <strong>creategraph();</strong><br /></font>
						<font style="BACKGROUND-COLOR: #f5f5dc">
								<strong> display();<br /></strong> for(int i=1;i&lt;=Max_vertex;i++)<br /> visited[i]=0; <br /> cout&lt;&lt;"请输入深度优先搜索的顶点:";<br /> cin&gt;&gt;k;<br /> cout&lt;&lt;endl;<br /> while(yn=='y') <br /> {<br /></font>
						<font style="BACKGROUND-COLOR: #f5f5dc">
								<strong> dfs(k);<br /></strong> cout&lt;&lt;"要继续遍历吗(Y/N)?";<br /> cin&gt;&gt;yn;<br /> }<br /> <br />    <br />}<br /></font>
				</font>
		</p>
<img src ="http://www.cppblog.com/snowhill/aggbug/16234.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-10 18:55 <a href="http://www.cppblog.com/snowhill/articles/16234.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++中的引用</title><link>http://www.cppblog.com/snowhill/articles/16219.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Sun, 10 Dec 2006 06:34:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/articles/16219.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/16219.html</wfw:comment><comments>http://www.cppblog.com/snowhill/articles/16219.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/16219.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/16219.html</trackback:ping><description><![CDATA[
		<div class="tit">
				<strong>
						<font style="BACKGROUND-COLOR: #ffa500">C++引用</font>
				</strong>
		</div>
		<div class="date"> </div>
		<table style="TABLE-LAYOUT: fixed">
				<tbody>
						<tr>
								<td>
										<div class="cnt">引用是C++引入的新语言特性，是C++常用的一个重要内容之一，正确、灵活地使用引用，可以使程序简洁、高效。我在工作中发现，许多人使用它仅仅是想当然，在某些微妙的场合，很容易出错，究其原由，大多因为没有搞清本源。故在本篇中我将对引用进行详细讨论，希望对大家更好地理解和使用引用起到抛砖引玉的作用。<br /><br />　　<font color="#a52a2a"><strong>引用简介</strong><br /></font><br />　　引用就是某一变量（目标）的一个别名，对引用的操作与对变量直接操作完全一样。<br /><br />　　引用的声明方法：类型标识符 &amp;引用名=目标变量名；<br /><br />　　【例1】：int a; int &amp;ra=a; //定义引用ra,它是变量a的引用，即别名<br /><br />　　说明：<br /><br />　　（1）&amp;在此不是求地址运算，而是起标识作用。<br /><br />　　（2）类型标识符是指目标变量的类型。<br /><br />　　<font color="#6633ff">（3）声明引用时，必须同时对其进行初始化。<br /></font><br />　　（<font color="#6633ff">4）引用声明完毕后，相当于目标变量名有两个名称，即该目标原名称和引用名，且不能再把该引用名作为其他变量名的别名</font>。<br /><br />　　 ra=1; 等价于 a=1; <br /><br />　　<font color="#0000ff">（5）声明一个引用，不是新定义了一个变量，它只表示该引用名是目标变量名的一个别名，它本身不是一种数据类型，因此引用本身不占存储单元，系统也不给引用分配存储单元。故：对引用求地址，就是对目标变量求地址。&amp;ra与&amp;a相等。</font><br /><br />　　<font color="#7fffd4"><font color="#0000ff">（6）不能建立数组的引用。因为数组是一个由若干个元素所组成的集合，所以无法建立一个数组的别名。</font><br /></font><br />　　引用应用<br /><br />　　1、引用作为参数 <br /><br />　　引用的一个重要作用就是作为函数的参数。以前的C语言中函数参数传递是值传递，如果有大块数据作为参数传递的时候，采用的方案往往是指针，因为这样可以避免将整块数据全部压栈，可以提高程序的效率。但是现在（C++中）又增加了一种同样有效率的选择（在某些特殊情况下又是必须的选择），就是引用。<br /><br />　　【例2】：<br /><br />void swap(int &amp;p1, int &amp;p2) //此处函数的形参p1, p2都是引用 <br />{ int p; p=p1; p1=p2; p2=p; } <br /><br />　　为在程序中调用该函数，则相应的主调函数的调用点处，直接以变量作为实参进行调用即可，而不需要实参变量有任何的特殊要求。如：对应上面定义的swap函数，相应的主调函数可写为：<br /><br />main( )<br />{ <br />　int a,b;<br />　cin&gt;&gt;a&gt;&gt;b; //输入a,b两变量的值<br />　swap(a,b); //直接以变量a和b作为实参调用swap函数 <br />　cout&lt;&lt;a&lt;&lt; ' ' &lt;&lt;b; //输出结果 <br />} <br /><br />　　上述程序运行时，如果输入数据10 20并回车后，则输出结果为20 10。<br /><br />　　由【例2】可看出：<br /><br />　　（1）传递引用给函数与传递指针的效果是一样的。这时，被调函数的形参就成为原来主调函数中的实参变量或对象的一个别名来使用，所以在被调函数中对形参变量的操作就是对其相应的目标对象（在主调函数中）的操作。<br /><br />　　（2）使用引用传递函数的参数，在内存中并没有产生实参的副本，它是直接对实参操作；而使用一般变量传递函数的参数，当发生函数调用时，需要给形参分配存储单元，形参变量是实参变量的副本；如果传递的是对象，还将调用拷贝构造函数。因此，当参数传递的数据较大时，用引用比用一般变量传递参数的效率和所占空间都好。<br /><br />　　（3）使用指针作为函数的参数虽然也能达到与使用引用的效果，但是，在被调函数中同样要给形参分配存储单元，且需要重复使用'*指针变量名'的形式进行运算，这很容易产生错误且程序的阅读性较差；另一方面，在主调函数的调用点处，必须用变量的地址作为实参。而引用更容易使用，更清晰。<br /><br />　　如果既要利用引用提高程序的效率，又要保护传递给函数的数据不在函数中被改变，就应使用常引用。<br /><br />　　2、常引用<br />　　常引用声明方式：const 类型标识符 &amp;引用名=目标变量名；<br /><br />　　用这种方式声明的引用，不能通过引用对目标变量的值进行修改,从而使引用的目标成为const，达到了引用的安全性。<br /><br />　　【例3】： <br /><br />int a ;<br />const int &amp;ra=a;<br />ra=1; //错误<br />a=1; //正确 <br /><br />　　这不光是让代码更健壮，也有些其它方面的需要。<br /><br />　　【例4】：假设有如下函数声明：<br /><br />string foo( );<br />void bar(string &amp; s); <br /><br />　　那么下面的表达式将是非法的：<br /><br />bar(foo( ));<br />bar('hello world'); <br /><br />　　原因在于foo( )和'hello world'串都会产生一个临时对象，而在C++中，这些临时对象都是const类型的。因此上面的表达式就是试图将一个const类型的对象转换为非const类型，这是非法的。<br /><br />　　引用型参数应该在能被定义为const的情况下，尽量定义为const 。<br /><br /><br />　　3、引用作为返回值 <br /><br />　　要以引用返回函数值，则函数定义时要按以下格式：<br /><br />类型标识符 &amp;函数名（形参列表及类型说明）<br />{函数体}<br /><br />　　说明：<br /><br />　　（1）以引用返回函数值，定义函数时需要在函数名前加&amp;<br /><br />　　（2）用引用返回一个函数值的最大好处是，在内存中不产生被返回值的副本。<br /><br />　　【例5】以下程序中定义了一个普通的函数fn1（它用返回值的方法返回函数值），另外一个函数fn2，它以引用的方法返回函数值。<br /><br />#include &lt;iostream.h&gt;<br />float temp; //定义全局变量temp<br />float fn1(float r); //声明函数fn1<br />float &amp;fn2(float r); //声明函数fn2<br />float fn1(float r) //定义函数fn1，它以返回值的方法返回函数值<br />{ <br />　temp=(float)(r*r*3.14); <br />　return temp; <br />}<br />float &amp;fn2(float r) //定义函数fn2，它以引用方式返回函数值<br />{ <br />　temp=(float)(r*r*3.14); <br />　return temp;<br />}<br />void main() //主函数<br />{ <br />　float a=fn1(10.0); //第1种情况，系统生成要返回值的副本（即临时变量）<br />　float &amp;b=fn1(10.0); //第2种情况，可能会出错（不同 C++系统有不同规定）<br />　//不能从被调函数中返回一个临时变量或局部变量的引用<br />　float c=fn2(10.0); //第3种情况，系统不生成返回值的副本<br />　//可以从被调函数中返回一个全局变量的引用<br />　float &amp;d=fn2(10.0); //第4种情况，系统不生成返回值的副本<br />　//可以从被调函数中返回一个全局变量的引用<br />　cout&lt;&lt;a&lt;&lt;c&lt;&lt;d;<br />} <br /><br />　　引用作为返回值，必须遵守以下规则：<br /><br />　　（1）不能返回局部变量的引用。这条可以参照Effective C++[1]的Item 31。主要原因是局部变量会在函数返回后被销毁，因此被返回的引用就成为了'无所指'的引用，程序会进入未知状态。 <br /><br />　　（2）不能返回函数内部new分配的内存的引用。这条可以参照Effective C++[1]的Item 31。虽然不存在局部变量的被动销毁问题，可对于这种情况（返回函数内部new分配内存的引用），又面临其它尴尬局面。例如，被函数返回的引用只是作为一个临时变量出现，而没有被赋予一个实际的变量，那么这个引用所指向的空间（由new分配）就无法释放，造成memory leak。<br /><br />　　（3）可以返回类成员的引用，但最好是const。这条原则可以参照Effective C++[1]的Item 30。主要原因是当对象的属性是与某种业务规则（business rule）相关联的时候，其赋值常常与某些其它属性或者对象的状态有关，因此有必要将赋值操作封装在一个业务规则当中。如果其它对象可以获得该属性的非常量引用（或指针），那么对该属性的单纯赋值就会破坏业务规则的完整性。<br /><br />　　（4）引用与一些操作符的重载：<br /><br />　　流操作符&lt;&lt;和&gt;&gt;，这两个操作符常常希望被连续使用，例如：cout &lt;&lt; 'hello' &lt;&lt; endl;　因此这两个操作符的返回值应该是一个仍然支持这两个操作符的流引用。可选的其它方案包括：返回一个流对象和返回一个流对象指针。但是对于返回一个流对象，程序必须重新（拷贝）构造一个新的流对象，也就是说，连续的两个&lt;&lt;操作符实际上是针对不同对象的！这无法让人接受。对于返回一个流指针则不能连续使用&lt;&lt;操作符。因此，返回一个流对象引用是惟一选择。这个唯一选择很关键，它说明了引用的重要性以及无可替代性，也许这就是C++语言中引入引用这个概念的原因吧。 赋值操作符=。这个操作符象流操作符一样，是可以连续使用的，例如：x = j = 10;或者(x=10)=100;赋值操作符的返回值必须是一个左值，以便可以被继续赋值。因此引用成了这个操作符的惟一返回值选择。<br /><br />　　【例6】 测试用返回引用的函数值作为赋值表达式的左值。<br /><br />#include &lt;iostream.h&gt;<br />int &amp;put(int n);<br />int vals[10];<br />int error=-1;<br />void main()<br />{<br />put(0)=10; //以put(0)函数值作为左值，等价于vals[0]=10; <br />put(9)=20; //以put(9)函数值作为左值，等价于vals[9]=10; <br />cout&lt;&lt;vals[0]; <br />cout&lt;&lt;vals[9];<br />} <br />int &amp;put(int n)<br />{<br />if (n&gt;=0 &amp;&amp; n&lt;=9 ) return vals[n]; <br />else { cout&lt;&lt;'subscript error'; return error; }<br />} <br /><br />　　（5）在另外的一些操作符中，却千万不能返回引用：+-*/ 四则运算符。它们不能返回引用，Effective C++[1]的Item23详细的讨论了这个问题。主要原因是这四个操作符没有side effect，因此，它们必须构造一个对象作为返回值，可选的方案包括：返回一个对象、返回一个局部变量的引用，返回一个new分配的对象的引用、返回一个静态对象引用。根据前面提到的引用作为返回值的三个规则，第2、3两个方案都被否决了。静态对象的引用又因为((a+b) == (c+d))会永远为true而导致错误。所以可选的只剩下返回一个对象了。<br /><br />　　4、引用和多态 <br /><br />　　引用是除指针外另一个可以产生多态效果的手段。这意味着，一个基类的引用可以指向它的派生类实例。<br /><br />　　【例7】： <br /><br />class 　A;<br />class 　B：public A{……};<br />B 　b;<br />A 　&amp;Ref = b; // 用派生类对象初始化基类对象的引用<br /><br />　　Ref 只能用来访问派生类对象中从基类继承下来的成员，是基类引用指向派生类。如果A类中定义有虚函数，并且在B类中重写了这个虚函数，就可以通过Ref产生多态效果。<br /><br />　　引用总结<br /><br />　　1）在引用的使用中，单纯给某个变量取个别名是毫无意义的，引用的目的主要用于在函数参数传递中，解决大块数据或对象的传递效率和空间不如意的问题。<br /><br />　　<font color="#008000">2）用引用传递函数的参数，能保证参数传递中不产生副本，提高传递的效率，且通过const的使用，保证了引用传递的安全性。</font><br /><br />　　3）引用与指针的区别是，指针通过某个指针变量指向一个对象后，对它所指向的变量间接操作。程序中使用指针，程序的可读性差；而引用本身就是目标变量的别名，对引用的操作就是对目标变量的操作。<br /><br />　　4）使用引用的时机。流操作符&lt;&lt;和&gt;&gt;、赋值操作符=的返回值、拷贝构造函数的参数、赋值操作符=的参数、其它情况都推荐使用引用<br />转自:http://hi.baidu.com/pavelliu/blog/category/c%26%2347%3Bc%2B%2B/index/1</div>
								</td>
						</tr>
				</tbody>
		</table>
<img src ="http://www.cppblog.com/snowhill/aggbug/16219.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-10 14:34 <a href="http://www.cppblog.com/snowhill/articles/16219.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>quicksort</title><link>http://www.cppblog.com/snowhill/articles/16026.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Tue, 05 Dec 2006 12:58:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/articles/16026.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/16026.html</wfw:comment><comments>http://www.cppblog.com/snowhill/articles/16026.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/16026.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/16026.html</trackback:ping><description><![CDATA[/*从小到大*/<br />void quicksort(a[n],left,right)<br />{<br /> int temp,i=left,j=right;<br /> temp=a[0];<br /> while(i&lt;j)<br /> {<br />  while((a[j]&gt;temp)&amp;&amp;(j&gt;i))  j--;<br />  a[i]=a[j];<br />  while((a[i]&lt;temp&amp;&amp;(j&gt;i)   i++;<br />  a[j]=a[i];<br /> }<br /> a[i]=temp;<br /> if(left&lt;i-1)<br />  quicksoft(a,left,i-1);<br /> if(right&gt;i+1)<br />  quicksort(a,i+1,right);<br />}<br />  <br />  <img src ="http://www.cppblog.com/snowhill/aggbug/16026.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-05 20:58 <a href="http://www.cppblog.com/snowhill/articles/16026.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>link node</title><link>http://www.cppblog.com/snowhill/articles/15912.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Sun, 03 Dec 2006 01:07:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/articles/15912.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/15912.html</wfw:comment><comments>http://www.cppblog.com/snowhill/articles/15912.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/15912.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/15912.html</trackback:ping><description><![CDATA[
		<p>// DATE:2006-11-20 BY:Snowhill<br />#include "iostream.h"<br />struct link<br />{<br /> int data;<br /> link *next;<br />};<br />//头插法建立链表,带头结点</p>
		<p>link *hcreat()<br />{<br />link *p,*s;<br />int i;<br />cin&gt;&gt;i;<br />p=new link;<br />p-&gt;next=NULL;<br />while(i)<br />{<br /> s=new link;<br /> s-&gt;data=i;<br /> s-&gt;next=p-&gt;next;<br /> p-&gt;next=s;<br /> cin&gt;&gt;i;<br />}<br />return p;</p>
		<p>}<br />//create the node list with rear<br />link *rcreat()<br />{<br /> link *s,*p,*r;<br /> int i;<br /> p=r=new link;//the p and r is head<br /> p-&gt;next=NULL;<br /> cout&lt;&lt;"thes adress of p-&gt;next is "&lt;&lt;p-&gt;next;<br /> cin&gt;&gt;i;<br /> while(i)<br /> {<br />  s=new link;<br />  s-&gt;data=i;<br />  r-&gt;next=s;<br />  r=s;<br />  cin&gt;&gt;i;<br /> }<br /> r-&gt;next=NULL;<br /> cout&lt;&lt;"thes adress of r-&gt;next is "&lt;&lt;p-&gt;next;<br /> return p;<br />}</p>
		<p>//print the link node<br />void print(link *head)<br />{<br /> link *p;<br /> p=head-&gt;next;<br /> while(p-&gt;next!=NULL)<br /> {<br />  cout&lt;&lt;p-&gt;data&lt;&lt;"-&gt;";<br />  p=p-&gt;next;<br /> }<br /> cout&lt;&lt;p-&gt;data;<br /> cout&lt;&lt;endl;<br />}</p>
		<p> </p>
		<p>void <font style="BACKGROUND-COLOR: #800080" color="#ff0066"><font style="BACKGROUND-COLOR: #f5f5dc">main()</font><br /></font>{<br />    link *p;<br />    p=rcreat();<br />    print(p);<br />    <br />}</p>
<img src ="http://www.cppblog.com/snowhill/aggbug/15912.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-03 09:07 <a href="http://www.cppblog.com/snowhill/articles/15912.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>