﻿<?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++博客-我辈岂是蓬蒿人！</title><link>http://www.cppblog.com/keyws/</link><description>C++ &amp;&amp; keyWordSpotting</description><language>zh-cn</language><lastBuildDate>Tue, 07 Apr 2026 13:25:26 GMT</lastBuildDate><pubDate>Tue, 07 Apr 2026 13:25:26 GMT</pubDate><ttl>60</ttl><item><title>【code】一个可以精确到毫秒的计时程序代码</title><link>http://www.cppblog.com/keyws/archive/2006/08/29/11810.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Tue, 29 Aug 2006 01:51:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/29/11810.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11810.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/29/11810.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11810.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11810.html</trackback:ping><description><![CDATA[
		<p> </p>
		<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: #000000">#ifndef _GHH_TIMER_GHH_<br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> _GHH_TIMER_GHH_    1</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"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> File: ghhTimer.h<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Date: 2006.08.14<br /></span><span style="COLOR: #008080"> </span><span style="COLOR: #008080">7</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">ctime</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 类导出导入类别的符号定义</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">#ifdef _DLL_FILE_<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> PORTTYE __declspec(dllexport) </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 导出</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#else</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> PORTTYE __declspec(dllimport) </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 导入</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> end of _DLL_FILE_</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">16</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">***************************************************************************<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #008000"> * 类名称    ghhTimer<br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #008000"> * <br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #008000"> * 描述<br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #008000"> *    本类对标准库计时函数进行了封装，可以实现非常精确的计时，毫秒级别<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #008000"> *<br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #008000"> * 使用说明<br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #008000"> *    在所要计时程序段之前，调用Start函数，程序段结束时，调用Pause函数，<br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #008000"> *    多次调用程序段，即可以比较精确的估计程序段的运行时间<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #008000">***************************************************************************</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">  PORTTYE ghhTimer<br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">29</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">30</span> <span style="COLOR: #000000">    ghhTimer();<br /></span><span style="COLOR: #008080">31</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">32</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">33</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> Start(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">); <br /></span><span style="COLOR: #008080">34</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> Stop(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">);    <br /></span><span style="COLOR: #008080">35</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> Pause(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">36</span> <span style="COLOR: #000000">    size_t GetSeconds(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">37</span> <span style="COLOR: #000000">    size_t GetMiliSeconds(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">38</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">39</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">40</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">enum</span><span style="COLOR: #000000"> {run </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">, stop, pause} _Status;<br /></span><span style="COLOR: #008080">41</span> <span style="COLOR: #000000">    time_t _Clock;<br /></span><span style="COLOR: #008080">42</span> <span style="COLOR: #000000">    time_t _TotalClocks;<br /></span><span style="COLOR: #008080">43</span> <span style="COLOR: #000000">};<br /></span><span style="COLOR: #008080">44</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">45</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> end of _GHH_TIMER_GHH_</span></div>
		<br />
		<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: #000000">#ifndef _DLL_FILE_<br /></span><span style="COLOR: #008080">  2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> _DLL_FILE_</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">  3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">  4</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ghhTimer.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">  5</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">  6</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">***************************************************************************<br /></span><span style="COLOR: #008080">  7</span> <span style="COLOR: #008000"> * about the important function "clock()"<br /></span><span style="COLOR: #008080">  8</span> <span style="COLOR: #008000"> * #include &lt;time.h&gt;<br /></span><span style="COLOR: #008080">  9</span> <span style="COLOR: #008000"> * clock_t clock( void );<br /></span><span style="COLOR: #008080"> 10</span> <span style="COLOR: #008000"> * The clock() function returns the processor time since the program started, <br /></span><span style="COLOR: #008080"> 11</span> <span style="COLOR: #008000"> * or -1 if that information is unavailable. <br /></span><span style="COLOR: #008080"> 12</span> <span style="COLOR: #008000"> * To convert the return value to seconds, divide it by CLOCKS_PER_SEC. <br /></span><span style="COLOR: #008080"> 13</span> <span style="COLOR: #008000"> * (Note: if your compiler is POSIX compliant, <br /></span><span style="COLOR: #008080"> 14</span> <span style="COLOR: #008000"> * then CLOCKS_PER_SEC is always defined as 1000000.)<br /></span><span style="COLOR: #008080"> 15</span> <span style="COLOR: #008000"> **************************************************************************</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 16</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 17</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 18</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 构造函数，设置初始状态</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 19</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">ghhTimer::ghhTimer() : _Status(stop), _Clock(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">), _TotalClocks(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080"> 20</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 21</span> <span style="COLOR: #000000">}<br /></span><span style="COLOR: #008080"> 22</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 23</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 当表已经停止或者暂停时启动停表，成功返回true,否则返回false</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 24</span> <span style="COLOR: #008000"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> ghhTimer::Start(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080"> 25</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 26</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000"> (_Status)<br /></span><span style="COLOR: #008080"> 27</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080"> 28</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> stop :<br /></span><span style="COLOR: #008080"> 29</span> <span style="COLOR: #000000">        _TotalClocks </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 30</span> <span style="COLOR: #000000">        _Clock </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> clock();<br /></span><span style="COLOR: #008080"> 31</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<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">case</span><span style="COLOR: #000000"> pause :<br /></span><span style="COLOR: #008080"> 34</span> <span style="COLOR: #000000">        _Clock </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> clock();<br /></span><span style="COLOR: #008080"> 35</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 36</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 37</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> run :<br /></span><span style="COLOR: #008080"> 38</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 39</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 40</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">default</span><span style="COLOR: #000000"> :<br /></span><span style="COLOR: #008080"> 41</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</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"><br /></span><span style="COLOR: #008080"> 44</span> <span style="COLOR: #000000">    _Status </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> run;<br /></span><span style="COLOR: #008080"> 45</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 46</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 47</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"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 表运行时暂停计时,成功返回true，否则返回false</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 50</span> <span style="COLOR: #008000"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> ghhTimer::Pause(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080"> 51</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 52</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000"> (_Status)<br /></span><span style="COLOR: #008080"> 53</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080"> 54</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> stop :<br /></span><span style="COLOR: #008080"> 55</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> pause :<br /></span><span style="COLOR: #008080"> 56</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</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">case</span><span style="COLOR: #000000"> run :<br /></span><span style="COLOR: #008080"> 59</span> <span style="COLOR: #000000">        _TotalClocks </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000"> (clock() </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> _Clock);<br /></span><span style="COLOR: #008080"> 60</span> <span style="COLOR: #000000">        _Clock </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 61</span> <span style="COLOR: #000000">        _Status </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> pause;<br /></span><span style="COLOR: #008080"> 62</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 63</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 64</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">default</span><span style="COLOR: #000000"> :<br /></span><span style="COLOR: #008080"> 65</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 66</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080"> 67</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 68</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 69</span> <span style="COLOR: #000000">}<br /></span><span style="COLOR: #008080"> 70</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 71</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 表运行或暂停时停止计时</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 72</span> <span style="COLOR: #008000"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> ghhTimer::Stop(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080"> 73</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 74</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000"> (_Status)<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">case</span><span style="COLOR: #000000"> stop :<br /></span><span style="COLOR: #008080"> 77</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> pause :<br /></span><span style="COLOR: #008080"> 78</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 79</span> <span style="COLOR: #000000">        <br /></span><span style="COLOR: #008080"> 80</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> run :<br /></span><span style="COLOR: #008080"> 81</span> <span style="COLOR: #000000">        _TotalClocks </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">(clock() </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> _Clock);<br /></span><span style="COLOR: #008080"> 82</span> <span style="COLOR: #000000">        _Clock </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 83</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 84</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 85</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">default</span><span style="COLOR: #000000"> :<br /></span><span style="COLOR: #008080"> 86</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<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">    _Status </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> stop;<br /></span><span style="COLOR: #008080"> 90</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080"> 91</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</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"><br /></span><span style="COLOR: #008080"> 94</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 得到当前积累的秒数</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 95</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">size_t ghhTimer::GetSeconds(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 96</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 97</span> <span style="COLOR: #000000">    time_t Clocks;<br /></span><span style="COLOR: #008080"> 98</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 99</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000"> (_Status)<br /></span><span style="COLOR: #008080">100</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">101</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> stop:<br /></span><span style="COLOR: #008080">102</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> pause:<br /></span><span style="COLOR: #008080">103</span> <span style="COLOR: #000000">        Clocks </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> _TotalClocks;<br /></span><span style="COLOR: #008080">104</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">105</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">106</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> run:<br /></span><span style="COLOR: #008080">107</span> <span style="COLOR: #000000">        Clocks </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> _TotalClocks </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> clock() </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> _Clock;<br /></span><span style="COLOR: #008080">108</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">109</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">110</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">default</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">111</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">112</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">113</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (Clocks </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> CLOCKS_PER_SEC);<br /></span><span style="COLOR: #008080">114</span> <span style="COLOR: #000000">}<br /></span><span style="COLOR: #008080">115</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">116</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 得到当前积累的毫秒数</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">117</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">size_t ghhTimer::GetMiliSeconds(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">118</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">119</span> <span style="COLOR: #000000">    time_t Clocks;<br /></span><span style="COLOR: #008080">120</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">121</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000">(_Status)<br /></span><span style="COLOR: #008080">122</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">123</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> stop:<br /></span><span style="COLOR: #008080">124</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> pause:<br /></span><span style="COLOR: #008080">125</span> <span style="COLOR: #000000">        Clocks </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> _TotalClocks;<br /></span><span style="COLOR: #008080">126</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">127</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> run:<br /></span><span style="COLOR: #008080">128</span> <span style="COLOR: #000000">        Clocks </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> _TotalClocks </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> clock() </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> _Clock;<br /></span><span style="COLOR: #008080">129</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">130</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">131</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">default</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">132</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">133</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">134</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (Clocks </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> CLOCKS_PER_SEC);<br /></span><span style="COLOR: #008080">135</span> <span style="COLOR: #000000">}<br /></span><span style="COLOR: #008080">136</span> <span style="COLOR: #000000"></span></div>
<img src ="http://www.cppblog.com/keyws/aggbug/11810.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-29 09:51 <a href="http://www.cppblog.com/keyws/archive/2006/08/29/11810.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【note】Effective C++ (2) - Shifting from C to C++</title><link>http://www.cppblog.com/keyws/archive/2006/08/22/11580.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Tue, 22 Aug 2006 09:24:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/22/11580.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11580.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/22/11580.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11580.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11580.html</trackback:ping><description><![CDATA[
		<h1 style="MARGIN: 17pt 0cm 16.5pt">
				<span lang="EN-US">Shifting from C to C++</span>
		</h1>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">1. To C++ programmer, for example, a pointer to a pointer looks a little funny. Why, we wonder, wasn’t a reference to a pointer used <span style="mso-spacerun: yes"> </span>instead?</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char chr[] = "chenzhenshi&amp;guohonghua";</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char*<span style="mso-spacerun: yes">  </span>pchr = chr;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char** ppchr = &amp;pchr; </span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char*&amp;<span style="mso-spacerun: yes">  </span>rpchr<span style="mso-spacerun: yes">  </span>= pchr; // a reference to a pointer</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pchr &lt;&lt; ' ' &lt;&lt; *ppchr &lt;&lt; ' ' &lt;&lt; rpchr &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">2. C is a fairly simple language. All it really offers is <span style="COLOR: green">macros, pointers, structs, arrays, and functions</span>. No matter what the problem is, the solution will always boil down to macros, pointers, structs, arrays, and functions. Not so in C++. The macros, pointers, structs, arrays and functions are still there, of course, but so are <span style="COLOR: green">private and protected members, function overloading, default parameters, constructors and destructors, user-defined operators, inline functions, references, friends, templates, exceptions, namespaces, and more</span>. The design space is much richer in C++ than it is in C: there are just a lot more options to consider.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 1: Prefer const and inline to #define</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">3. The Item might better be called “prefer the compiler to the preprocessor”.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">4. <span style="mso-tab-count: 1">   </span>const char* pc;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>pc = a1;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pc &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>pc = a2;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pc &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char* const pcc = "a const pointer to a const char array";</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pcc &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>// error C2166: l-value specifies const object</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>// pcc = a1;<span style="mso-spacerun: yes">  </span>// error!</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pcc &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">5. You can define a const variable in a class, but it must be static const, and have a definition in an implementation file. </span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">// .h file</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">class try_const</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">{</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">public:</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>static const int num;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">};</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">// .cxx file</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">const int try_const::num = 250;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">6. You can get all the efficiency of a macro plus all the predictable behavior and type safety of a regular function by using an inline function.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">Template &lt;class type&gt;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">Inline const type&amp; max (const type&amp; a, const type&amp; b)</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">{</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">Return a &gt; b ? a : b ;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">}</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">7. Given the availability of consts and inlines, your need for the preprocessor is reduced, but it's not completely eliminated. The day is far from near when you can abandon #include, and #ifdef/#ifndef continue to play important roles in controlling compilation. It's not yet time to retire the preprocessor, but you should definitely plan to start giving it longer and more frequent vacations.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 2: Prefer &lt;iostream&gt; to &lt;stdio.h&gt;</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">8. <span style="mso-spacerun: yes"> </span>scanf and printf are not type-safe and extensible.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">9.<span style="mso-spacerun: yes">  </span>In particular, if you #include &lt;iostream&gt;, you get the elements of the iostream library ensconced within the namespace std (see Item 28), but if you #include &lt;iostream.h&gt;, you get those same elements at global scope. Getting them at global scope can lead to name conflicts, precisely the kinds of name conflicts the use of namespaces is designed to prevent.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 3: Prefer new and delete to malloc and free</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">10. The problem with malloc and free(and their variants) is simple : they don’t know about constructors and destructors.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">11. free</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">操作不会调用析构函数，如果指针所指对象本身又分配了内存，则会造成内存丢失。</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 4: Prefer C++ style comments</font>
				</span>
		</h2>
		<h1 style="MARGIN: 17pt 0cm 16.5pt">
				<span lang="EN-US">Memory Management</span>
		</h1>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">12. Memory management concerns in C++ fall into two general camps: getting it right and making it perform efficiently.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 5: Use the same form in corresponding uses of new and delete</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">13. When you use new, two things happen.</span>
				<span lang="EN-US" style="mso-font-kerning: 0pt">
				</span>
				<span lang="EN-US">First, memory is allocated. Second, one or more constructors are called for that memory. When you use delete, two other things happen: one or more destructors are called for the memory, then the memory is deallocated.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">14. The standard C++ library includes string and vector templates that reduce the need for built-in arrays to nearly zero.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 6: Use delete on pointer members in destructors</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">15. Speaking of smart pointers, one way to avoid the need to delete pointer members is to replace those members with smart pointer objects like the standard C++ Library’s auto_ptr.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 7: Be prepared for out-of-memory conditions</font>
				</span>
		</h2>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 8: Adhere to convention when writing operator new and operator delete</font>
				</span>
		</h2>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 9: Avoid hiding the “normal” form of new</font>
				</span>
		</h2>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 10: Write operator delete if you write operator new</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">让我们回过头去看看这样一个基本问题：为什么有必要写自己的</span>
				<span lang="EN-US">operator new</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span lang="EN-US">operator delete</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">？答案通常是：为了效率。缺省的</span>
				<span lang="EN-US">operator new</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span lang="EN-US">operator delete</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">具有非常好的通用性，它的这种灵活性也使得在某些特定的场合下，可以进一步改善它的性能。尤其在那些需要动态分配大量的但很小的对象的应用程序里，情况更是如此。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
<img src ="http://www.cppblog.com/keyws/aggbug/11580.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-22 17:24 <a href="http://www.cppblog.com/keyws/archive/2006/08/22/11580.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【note】《C++ Primer － 8.4节“动态分配的对象”》学习笔记 </title><link>http://www.cppblog.com/keyws/archive/2006/08/22/11573.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Tue, 22 Aug 2006 06:54:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/22/11573.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11573.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/22/11573.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11573.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11573.html</trackback:ping><description><![CDATA[
		<div class="postTitle" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span> <br /><span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">   动态分配的对象：程序员完全控制分配与释放，分配在程序的空闲存储区（<span lang="EN-US">free store)</span>的可用内存池中。<span lang="EN-US"><br /> <br /> 1</span>）单个对象的动态分配与释放；<span lang="EN-US"><br /> new</span>表达式没有返回实际分配的对象，而是返回指向该对象的指针。对该对象的全部操作都要通过这个指针间接完成。<span lang="EN-US"><br /> </span>随机分配的内存具有随机的位模式，建议初始化，例如：<span lang="EN-US"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><o:p></o:p></span></span></div>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> int* pi = new int(0);</span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<o:p>
						</o:p>
				</span>
		</p>
		<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 12pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
						<br /> </p>
		</span>
		<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">空闲存储区是有限的资源，若被耗尽，<span lang="EN-US">new</span>表达式会失败，抛出<span lang="EN-US">bad_alloc</span>异常。<span lang="EN-US"><br /> </span>这样做没有必要<span lang="EN-US">:<o:p></o:p></span></span>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">    </span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">if</span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> ( pi != 0 )<br />        delete pi;</span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">说明：如果指针操作数被设置为<span lang="EN-US">0</span>，则<span lang="EN-US">C++</span>保证<span lang="EN-US">delete</span>表达式不会调用操作符<span lang="EN-US">delete()</span>。所以没有必要测试其是否为<span lang="EN-US">0</span>。<span lang="EN-US"><br /> </span>在<span lang="EN-US">delete</span>表达式之后，<span lang="EN-US">pi</span>被称作空悬指针，即指向无效内存的指针。空悬指针是程序错误的根源，建议对象释放后，将该指针设置为<span lang="EN-US">0</span>。<span lang="EN-US"><br /> <br /> 2</span>）<span lang="EN-US">auto_ptr<br /> auto_ptr</span>是<span lang="EN-US">C++</span>标准库提供的类模板，它可以帮助程序员自动管理用<span lang="EN-US">new</span>表达式动态分配的单个对象，但是，它没有对数组管理提供类似支持。它的头文件为：<span lang="EN-US"><o:p></o:p></span></span> </p>
		<div class="postText">
				<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">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #000000">    #include </span>
						<span style="COLOR: #000000">&lt;</span>
						<span style="COLOR: #000000">memory</span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000"> </span>
				</div>
		</div>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">当<span lang="EN-US">auto_ptr</span>对象的生命期结束时，动态分配的对象被自动释放。<span lang="EN-US"><br /> auto_ptr</span>类模板背后的主要动机是支持与普通指针类型相同的语法，但是为<span lang="EN-US">auto_ptr</span>对象所指对象的释放提供自动管理。例：<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /?>
						<v:shape id="_x0000_i1026" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt="">
								<v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif">
								</v:imagedata>
						</v:shape>
						<span style="COLOR: black">    </span>
						<span style="COLOR: green">// </span>
				</span>
				<span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">第一种初始化形式<span lang="EN-US"><br /><v:shape id="_x0000_i1027" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span></span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">    std::auto_ptr&lt;</span>
				<font size="2">
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">int</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">&gt; pi( </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">new</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">int</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">(1024) );    </span>
						<font face="Verdana" color="#333333">
						</font>
				</font>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">// <v:shape id="_x0000_i1028" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span>
				<font size="2">
						<font color="#333333">
								<font face="Verdana">
										<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
												<o:p>
												</o:p>
										</span>
								</font>
						</font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> auto_ptr</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">类模板支持所有权概念，当一个<span lang="EN-US">auto_ptr</span>对象被用另一个<span lang="EN-US">auto_ptr</span>对象初始化赋值时，左边被赋值或初始化的对象就拥有了空闲存储区内底层对象的所有权，而右边的<span lang="EN-US">auto_ptr</span>对象则<font color="#ff0000">撤消所有责任</font>。例：<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<v:shape id="_x0000_i1029" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt="">
								<v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif">
								</v:imagedata>
						</v:shape>
						<span style="COLOR: black">    std::auto_ptr&lt;std::</span>
						<span style="COLOR: blue">string</span>
						<span style="COLOR: black">&gt; pstr_auto( </span>
						<span style="COLOR: blue">new</span>
						<span style="COLOR: black"> std::</span>
						<span style="COLOR: blue">string</span>
						<span style="COLOR: black">( "Brontonsaurus" ) );<br /><v:shape id="_x0000_i1030" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape>    </span>
						<font color="#333333">
						</font>
						<span style="COLOR: green">// </span>
						<font color="#333333">
						</font>
				</span>
				<span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">第二种初始化形式<span lang="EN-US"><br /><v:shape id="_x0000_i1031" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span></span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">    std::auto_ptr&lt;std::</span>
				<font size="2">
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">string</span>
						<font face="Verdana" color="#333333">
						</font>
				</font>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">&gt; pstr_auto2( pstr_auto );<v:shape id="_x0000_i1032" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span>
				<font size="2">
						<font color="#333333">
								<font face="Verdana">
										<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
												<o:p>
												</o:p>
										</span>
								</font>
						</font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">判断是否指向一个对象，例：<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
				</span>
		</p>
		<div class="postText">
				<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: #000000">    </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> 第三种初始化形式</span>
						<span style="COLOR: #008000">
								<br />
						</span>
						<span style="COLOR: #000000">    auto_ptr</span>
						<span style="COLOR: #000000">&lt;</span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000"> p_auto_int;    <br />    </span>
						<span style="COLOR: #0000ff">if</span>
						<span style="COLOR: #000000"> ( p_auto_int.</span>
						<span style="COLOR: #0000ff">get</span>
						<span style="COLOR: #000000">() </span>
						<span style="COLOR: #000000">==</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">0</span>
						<span style="COLOR: #000000"> )<br />        <img src="http://www.cppblog.com/images/dot.gif" /><br />    </span>
						<span style="COLOR: #0000ff">else</span>
						<span style="COLOR: #000000">
								<br />        </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> 重置底层指针，必须使用此函数        </span>
						<span style="COLOR: #008000">
								<br />
						</span>
						<span style="COLOR: #000000">        p_auto_int.reset( </span>
						<span style="COLOR: #0000ff">new</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000">( </span>
						<span style="COLOR: #000000">1024</span>
						<span style="COLOR: #000000"> ) );<br /></span>
				</div>
		</div>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<br /> 3<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">）数组的动态分配与释放<span lang="EN-US"><br /> </span>建议使用<span lang="EN-US">C++</span>标准库<span lang="EN-US">string,</span>避免使用<span lang="EN-US">C</span>风格字符串数组。<span lang="EN-US"><br /> </span>为避免动态分配数组的内存管理带来的问题，一般建议使用标准库<span lang="EN-US">vector</span>、<span lang="EN-US">list</span>或<span lang="EN-US">string</span>容器类型。<span lang="EN-US"><br /> <br /> 4</span>）常量对象的动态分配与释放<span lang="EN-US"><br /> </span>可以使用<span lang="EN-US">new</span>表达式在空闲存储区内创建一个<span lang="EN-US">const</span>对象，例：<span lang="EN-US"><o:p></o:p></span></span></p>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<v:shape id="_x0000_i1042" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt="">
								<v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif">
								</v:imagedata>
						</v:shape>
						<span style="COLOR: black">    </span>
						<span style="COLOR: green">// </span>
				</span>
				<span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">此时必须初始化，否则编译错误<span lang="EN-US"><br /><v:shape id="_x0000_i1043" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span></span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">    </span>
				<font size="2">
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">const</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">int</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">* pci = </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">new</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">const</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">int</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">(1024);    </span>
						<font color="#333333">
								<font face="Verdana">
										<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
												<o:p>
												</o:p>
										</span>
								</font>
						</font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">我们不能在空闲存储区创建内置类型元素的<span lang="EN-US">const</span>数组，原因是：我们不能初始化用<span lang="EN-US">new</span>表达式创建的内置类型数组的元素。例：<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<v:shape id="_x0000_i1044" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt="">
								<v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif">
								</v:imagedata>
						</v:shape>
						<span style="COLOR: black">    </span>
						<span style="COLOR: blue">const</span>
						<span style="COLOR: black"> </span>
						<span style="COLOR: blue">int</span>
						<span style="COLOR: black">* pci = </span>
						<span style="COLOR: blue">new</span>
						<span style="COLOR: black"> </span>
						<span style="COLOR: blue">const</span>
						<span style="COLOR: black"> </span>
						<span style="COLOR: blue">int</span>
						<span style="COLOR: black">[100]; </span>
						<span style="COLOR: green">// </span>
				</span>
				<span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">编译错误<span lang="EN-US"><v:shape id="_x0000_i1045" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span></span>
				<font size="2">
						<font color="#333333">
								<font face="Verdana">
										<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
												<o:p>
												</o:p>
										</span>
								</font>
						</font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<br /> 5</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">）定位<span lang="EN-US">new</span>表达式<span lang="EN-US"><br /> new</span>表达式的第三种形式允许程序员要求将对象创建在已经被分配好的内存中。称为：定位<span lang="EN-US">new</span>表达式（<span lang="EN-US">placement new expression)</span>。程序员在<span lang="EN-US">new</span>表达式中指定待创建对象所在的内存地址。如下所示：<span lang="EN-US"><br /> new </span>（<span lang="EN-US">place_address) type-specifier<br /> </span>注意：<span lang="EN-US">place_address</span>必须是个指针，必须包含头文件<span lang="EN-US">&lt;new&gt;</span>。这项设施允许程序员预分配大量的内存，供以后通过这种形式的<span lang="EN-US">new</span>表达式创建对象。例如：<span lang="EN-US"><o:p></o:p></span></span>
				<br />
		</p>
		<div class="postText">
				<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">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #000000">    #include </span>
						<span style="COLOR: #000000">&lt;</span>
						<span style="COLOR: #000000">iostream</span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    #include </span>
						<span style="COLOR: #000000">&lt;</span>
						<span style="COLOR: #0000ff">new</span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> 必须包含这个头文件</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">const</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> chunk </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">16</span>
						<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">class</span>
						<span style="COLOR: #000000"> Foo<br /><img id="Codehighlighter1_87_99_Open_Image" onclick="this.style.display='none'; Codehighlighter1_87_99_Open_Text.style.display='none'; Codehighlighter1_87_99_Closed_Image.style.display='inline'; Codehighlighter1_87_99_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_87_99_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_87_99_Closed_Text.style.display='none'; Codehighlighter1_87_99_Open_Image.style.display='inline'; Codehighlighter1_87_99_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_87_99_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.cppblog.com/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_87_99_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        <img src="http://www.cppblog.com/images/dot.gif" /><img src="http://www.cppblog.com/images/dot.gif" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000">*</span>
						<span style="COLOR: #000000"> buf </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">new</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000">[ </span>
						<span style="COLOR: #0000ff">sizeof</span>
						<span style="COLOR: #000000">(Foo) </span>
						<span style="COLOR: #000000">*</span>
						<span style="COLOR: #000000"> chunk ];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> main(</span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> argc, </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000">*</span>
						<span style="COLOR: #000000"> argv[])<br /><img id="Codehighlighter1_187_290_Open_Image" onclick="this.style.display='none'; Codehighlighter1_187_290_Open_Text.style.display='none'; Codehighlighter1_187_290_Closed_Image.style.display='inline'; Codehighlighter1_187_290_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_187_290_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_187_290_Closed_Text.style.display='none'; Codehighlighter1_187_290_Open_Image.style.display='inline'; Codehighlighter1_187_290_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_187_290_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.cppblog.com/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_187_290_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> 只有这种形式的创建，没有配对形式的delete </span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        Foo</span>
								<span style="COLOR: #000000">*</span>
								<span style="COLOR: #000000"> pb </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">new</span>
								<span style="COLOR: #000000"> (buf) Foo;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        <img src="http://www.cppblog.com/images/dot.gif" /><img src="http://www.cppblog.com/images/dot.gif" />        <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        delete[] buff;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						</span>
				</div>
		</div>
<img src ="http://www.cppblog.com/keyws/aggbug/11573.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-22 14:54 <a href="http://www.cppblog.com/keyws/archive/2006/08/22/11573.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【note】Effective C++ (1) - Introduction </title><link>http://www.cppblog.com/keyws/archive/2006/08/20/11495.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Sun, 20 Aug 2006 08:10:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/20/11495.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11495.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/20/11495.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11495.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11495.html</trackback:ping><description><![CDATA[
		<h1 style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">Introduction</span>
		</h1>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">1. A <span style="COLOR: green">declaration</span> tells compilers about the name and type of an object, function, class, or template, but it omits certain details.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">2. A <span style="COLOR: green">definition</span>, on the other hand, provides compilers with the details. For an object, the definition is where compilers allocate memory for the object. For a function or a function template, the definition provides the code body. For a class or a class template, the definition lists the members of the class or template.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">3. When you define a class, you generally need a default constructor if you want to define arrays of objects.Incidentally, if you want to create an array of objects for which there is no default constructor, the usual ploy is to define an array of pointers instead. Then you can initialize each pointer separately by using new.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">4. Probably the most important use of the copy constructor is to define what it means to pass and return objects by value.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">5. From a purely operational point of view, the difference between initialization and assignment is that the former is performed by a constructor while the latter is performed by operator=. In other words, the two processes correspond to different function calls. The reason for the distinction is that the two kinds of functions <span style="COLOR: green">must worry about different things</span>. Constructors usually have to check their arguments for validity, whereas most assignment operators can take it for granted that their argument is legitimate (because it has already been constructed). On the other hand, the target of an assignment, unlike an object undergoing construction, may already have resources allocated to it. These resources typically must be released before the new resources can be assigned. Frequently, one of these resources is memory. Before an assignment operator can allocate memory for a new value, it must first deallocate the memory that was allocated for the old value.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
		</p>
		<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">
				<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> a possible String constructor</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">String::String(</span>
				<span style="COLOR: #0000ff">const</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">char</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">value)<br /><img id="Codehighlighter1_67_287_Open_Image" onclick="this.style.display='none'; Codehighlighter1_67_287_Open_Text.style.display='none'; Codehighlighter1_67_287_Closed_Image.style.display='inline'; Codehighlighter1_67_287_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_67_287_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_67_287_Closed_Text.style.display='none'; Codehighlighter1_67_287_Open_Image.style.display='inline'; Codehighlighter1_67_287_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
				<span id="Codehighlighter1_67_287_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.cppblog.com/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_67_287_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">if</span>
						<span style="COLOR: #000000"> (value)<br /><img id="Codehighlighter1_82_175_Open_Image" onclick="this.style.display='none'; Codehighlighter1_82_175_Open_Text.style.display='none'; Codehighlighter1_82_175_Closed_Image.style.display='inline'; Codehighlighter1_82_175_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_82_175_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_82_175_Closed_Text.style.display='none'; Codehighlighter1_82_175_Open_Image.style.display='inline'; Codehighlighter1_82_175_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_82_175_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.cppblog.com/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_82_175_Open_Text">
								<span style="COLOR: #000000">{ <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> if value ptr isn't null</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        data </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">new</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">char</span>
								<span style="COLOR: #000000">[strlen(value) </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">1</span>
								<span style="COLOR: #000000">];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        strcpy(data,value);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">else</span>
						<span style="COLOR: #000000"> <br /><img id="Codehighlighter1_186_285_Open_Image" onclick="this.style.display='none'; Codehighlighter1_186_285_Open_Text.style.display='none'; Codehighlighter1_186_285_Closed_Image.style.display='inline'; Codehighlighter1_186_285_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_186_285_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_186_285_Closed_Text.style.display='none'; Codehighlighter1_186_285_Open_Image.style.display='inline'; Codehighlighter1_186_285_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_186_285_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.cppblog.com/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_186_285_Open_Text">
								<span style="COLOR: #000000">{ <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> handle null value ptr3</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        data </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">new</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">char</span>
								<span style="COLOR: #000000">[</span>
								<span style="COLOR: #000000">1</span>
								<span style="COLOR: #000000">];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #000000">*</span>
								<span style="COLOR: #000000">data </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">\0</span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">; </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> add trailing</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        </span>
								<span style="COLOR: #0000ff">null</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">char</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> a possible String assignment operator</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />String</span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000"> String::</span>
				<span style="COLOR: #0000ff">operator</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #0000ff">const</span>
				<span style="COLOR: #000000"> String</span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000"> rhs)<br /><img id="Codehighlighter1_377_593_Open_Image" onclick="this.style.display='none'; Codehighlighter1_377_593_Open_Text.style.display='none'; Codehighlighter1_377_593_Closed_Image.style.display='inline'; Codehighlighter1_377_593_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_377_593_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_377_593_Closed_Text.style.display='none'; Codehighlighter1_377_593_Open_Image.style.display='inline'; Codehighlighter1_377_593_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
				<span id="Codehighlighter1_377_593_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.cppblog.com/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_377_593_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">if</span>
						<span style="COLOR: #000000"> (</span>
						<span style="COLOR: #0000ff">this</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">==</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000">rhs)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">return</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">*</span>
						<span style="COLOR: #0000ff">this</span>
						<span style="COLOR: #000000">; </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> see Item 17</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    delete [] data; </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> delete old memory</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    data </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> allocate new memory</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">        </span>
						<span style="COLOR: #0000ff">new</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000">[strlen(rhs.data) </span>
						<span style="COLOR: #000000">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">1</span>
						<span style="COLOR: #000000">];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    strcpy(data, rhs.data);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">return</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">*</span>
						<span style="COLOR: #0000ff">this</span>
						<span style="COLOR: #000000">; </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> see Item 15</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />
						</span>
						<span style="COLOR: #000000">}</span>
				</span>
		</div>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<br />6. These different casting forms serve different purposes:</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="COLOR: green">const_cast</span>
				<span lang="EN-US"> is designed to cast away the constness of objects and pointers, a topic I examine in Item 21.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="COLOR: green">dynamic_cast</span>
				<span lang="EN-US"> is used to perform "safe downcasting," a subject we'll explore in Item 39.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="COLOR: green">reinterpret_cast</span>
				<span lang="EN-US"> is engineered for casts that yield <span style="COLOR: green">implementation-dependent results</span>, e.g., casting between function pointer types. (You're not likely to need reinterpret_cast very often. I don't use it at all in this book.)</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="COLOR: green">static_cast </span>
				<span lang="EN-US">is sort of the catch-all cast. It's what you use when none of the other casts is appropriate. <span style="COLOR: green">It's the closest in meaning to the conventional C-style casts.</span></span>
		</p>
<img src ="http://www.cppblog.com/keyws/aggbug/11495.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-20 16:10 <a href="http://www.cppblog.com/keyws/archive/2006/08/20/11495.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转】Operator Overloading</title><link>http://www.cppblog.com/keyws/archive/2006/08/17/11335.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Thu, 17 Aug 2006 04:14:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/17/11335.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11335.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/17/11335.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11335.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11335.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Thinking in C++, 2nd ed. Volume 1								©2000 by Bruce Eckel														Unary operators												The following example shows the syntax to overload all the unary operators, ...&nbsp;&nbsp;<a href='http://www.cppblog.com/keyws/archive/2006/08/17/11335.html'>阅读全文</a><img src ="http://www.cppblog.com/keyws/aggbug/11335.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-17 12:14 <a href="http://www.cppblog.com/keyws/archive/2006/08/17/11335.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【原】运算符重载与名字空间的困惑</title><link>http://www.cppblog.com/keyws/archive/2006/08/16/11304.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Wed, 16 Aug 2006 13:09:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/16/11304.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11304.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/16/11304.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11304.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11304.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">   学习数据结构，用到了输入输出运算符重载，结果编译之下，错误重重，，随即停止学习进度，追查祸源，在花费巨大脑力与时间成本之后，终于知自己错误之所在，定位于名字空间之困扰。为牢记教训，写一简化版本记录</span>
				<span lang="EN-US">debug</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">过程如下，警示自己。<br /><br /></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">
						<font color="#ff0033">问题</font>：<br />   </span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">   有如下两个代码文件：<br />   <span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">// 20060816_operator.cxx<br /></span></span>
		</p>
		<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: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">20060816_operator.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> argv[])<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> GHH(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">GuoHonghua</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">    Honghua ghh(GHH);<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> ghh </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> endl;<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">}</span></div>   
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US">// 20060816_operator.h<br /></span></p><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: #000000">#ifndef _20060816_OPERATOR_GHH_<br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> _20060816_OPERATOR_GHH_    1</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"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Author : GuoHonghua<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Date : 2006.08.16<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> File : 20060816_operator.h</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Honghua<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">    friend std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> os, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Honghua</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> ghh);<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    Honghua(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> ghh) : _ghh(ghh)<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">    std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> _ghh;<br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">};<br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> os, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Honghua</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> ghh)<br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000">    os </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> ghh._ghh;<br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> os;<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">}<br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> end of _20060816_OPERATOR_GHH_</span></div><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">用上面两个文件建立工程，</span><span lang="EN-US">vc6.0</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下编译会出错。报告如下：</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US">--------------------Configuration: 20060816_operator - Win32 Debug--------------------</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US">Compiling...</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US">20060816_operator.cxx</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US">f:\ghh_project\cxxdetail\20060816_operator.h(24) : error C2248: '_ghh' : cannot access private member declared in class 'Honghua'</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US"><span style="mso-spacerun: yes">        </span>f:\ghh_project\cxxdetail\20060816_operator.h(19) : see declaration of '_ghh'</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US">F:\ghh_project\CxxDetail\20060816_operator.cxx(8) : error C2593: 'operator &lt;&lt;' is ambiguous</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US">Error executing cl.exe.</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><o:p> </o:p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US">20060816_operator.exe - 2 error(s), 0 warning(s)<br /><span style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-bidi-font-family: 'Times New Roman'"><font color="#ff0066">解决</font>:<br />   </span></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><span lang="EN-US" style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore"><font face="Times New Roman">1．<span style="FONT: 7pt 'Times New Roman'">  </span></font></span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调整名字空间声明和自定义头文件的次序，如下：<br /></span></p><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: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">20060816_operator.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br /></span><span style="COLOR: #008080">4</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> argv[])<br /></span><span style="COLOR: #008080">5</span> <span style="COLOR: #000000"></span></div><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><span lang="EN-US" style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">2．<span style="FONT: 7pt 'Times New Roman'">  </span></span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">把类定义于标准名字之内，</span><span lang="EN-US">20060816_operator.h</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件修改如下（不推荐）<br /></span></p><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: #000000">#ifndef _20060816_OPERATOR_GHH_<br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> _20060816_OPERATOR_GHH_    1</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"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Author : GuoHonghua<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Date : 2006.08.16<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> File : 20060816_operator.h</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Honghua<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">        friend std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> os, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Honghua</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> ghh);<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">        Honghua(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> ghh) : _ghh(ghh)<br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">        {<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">        }<br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">        <br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">        std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> _ghh;<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">    };<br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">    std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> os, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Honghua</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> ghh)<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">        os </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> ghh._ghh;<br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> os;<br /></span><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">30</span> <span style="COLOR: #000000">}<br /></span><span style="COLOR: #008080">31</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> end of _20060816_OPERATOR_GHH_</span> <span style="COLOR: #000000"></span></div><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><span lang="EN-US" style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">3．<span style="FONT: 7pt 'Times New Roman'">  </span></span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">使用头文件</span><span lang="EN-US">iostream.h</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">代替</span><span lang="EN-US">iostream,</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">比较落后的方式（不推荐</span><span lang="EN-US">!</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）<br /></span></p><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"> 20060816_operator.cxx<br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> #include &lt;iostream&gt;</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #008000"></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"> 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">"</span><span style="COLOR: #000000">20060816_operator.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> argv[])<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> GHH(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">GuoHonghua</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">    Honghua ghh(GHH);<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> ghh </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> endl;<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">}</span> <span style="COLOR: #000000"></span></div><br /><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"> 20060816_operator.h</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">#ifndef _20060816_OPERATOR_GHH_<br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> _20060816_OPERATOR_GHH_    1</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Author : GuoHonghua<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Date : 2006.08.16<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> File : 20060816_operator.h<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> #include &lt;iostream&gt;</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #008000"></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">10</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Honghua<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    friend ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> os, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Honghua</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> ghh);<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">    Honghua(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> ghh) : _ghh(ghh)<br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">    std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> _ghh;<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">};<br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> os, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Honghua</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> ghh)<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">    os </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> ghh._ghh;<br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> os;<br /></span><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">}<br /></span><span style="COLOR: #008080">30</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">31</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> end of _20060816_OPERATOR_GHH_</span></div><br /><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><span lang="EN-US" style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">4．<span style="FONT: 7pt 'Times New Roman'">  </span></span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">任何时候都不使用</span><span lang="EN-US">using namespace </span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">声明！这是只需要修改</span><span lang="EN-US">20060816_operator.cxx</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件如下：<br /></span></p><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: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">20060816_operator.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> argv[])<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> const string GHH("GuoHonghua");</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> GHH(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">GuoHonghua</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">    Honghua ghh(GHH);<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> cout &lt;&lt; ghh &lt;&lt; endl;</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    std::cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> ghh </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> std::endl;<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">}</span></div><br /><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font color="#ff0066">体会</font>：<br />   名字空间本来就是为了解决名字冲突问题而引入，以使标准库不受外界影响。在这个意义上来说，</span><span lang="EN-US">using namespace std;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不是可以随意使用的语句，应该考虑到它的位置对程序的可能影响，而最彻底的杜绝错误的解决办法是在任何时候任何情况下都不使用此语句！</span></p><img src ="http://www.cppblog.com/keyws/aggbug/11304.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-16 21:09 <a href="http://www.cppblog.com/keyws/archive/2006/08/16/11304.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转】vector （二）</title><link>http://www.cppblog.com/keyws/archive/2006/08/13/11194.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Sun, 13 Aug 2006 11:52:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/13/11194.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11194.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/13/11194.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11194.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11194.html</trackback:ping><description><![CDATA[
		<table>
				<tbody>
						<tr>
								<td>
										<div class="body-content">
												<div class="header-box">
														<a href="http://www.cppreference.com/">www.cppreference.com</a>
														<br />
														<br />assign </div>
												<div class="syntax-name-format">Syntax: </div>
												<pre class="syntax-box">
														<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: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> assign( size_type num, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> TYPE</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> val );<br /></span><span style="COLOR: #008080">3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> assign( input_iterator start, input_iterator end );</span></div>
												</pre>
												<p>The assign() function either gives the current vector the values from <em>start</em> to <em>end</em>, or gives it <em>num</em> copies of <em>val</em>.</p>
												<p>This function <font color="#ff3333">will destroy the previous contents of the vector.</font></p>
												<p>For example, the following code uses assign() to put 10 copies of the integer 42 into a vector:</p>
												<pre class="example-code"> vector&lt;int&gt; v;
 v.assign( 10, 42 );
 for( int i = 0; i &lt; v.size(); i++ ) {
   cout &lt;&lt; v[i] &lt;&lt; " ";
 }
 cout &lt;&lt; endl;            
</pre>
												<p>The above code displays the following output:</p>
												<pre class="example-code"> 42 42 42 42 42 42 42 42 42 42          
</pre>
												<p>The next example shows how assign() can be used to copy one vector to another:</p>
												<pre class="example-code"> vector&lt;int&gt; v1;
 for( int i = 0; i &lt; 10; i++ ) {
   v1.push_back( i );
 }              

 vector&lt;int&gt; v2;
 v2.assign( v1.begin(), v1.end() );             

 for( int i = 0; i &lt; v2.size(); i++ ) {
   cout &lt;&lt; v2[i] &lt;&lt; " ";
 }
 cout &lt;&lt; endl;            
</pre>
												<p>When run, the above code displays the following output:</p>
												<pre class="example-code"> 0 1 2 3 4 5 6 7 8 9</pre>
										</div>
								</td>
						</tr>
				</tbody>
		</table>
		<hr />
		<div class="name-format">back </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">
				<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: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000">TYPE</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> back();<br /></span><span style="COLOR: #008080">3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> TYPE</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> back() </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">;</span></div>
		</pre>
		<p>The back() function <font color="#ff3300">returns a reference to the last element in the vector</font>.</p>
		<p>For example:</p>
		<pre class="example-code"> vector&lt;int&gt; v;
 for( int i = 0; i &lt; 5; i++ ) {
   v.push_back(i);
 }
 cout &lt;&lt; "The first element is " &lt;&lt; v.front()
      &lt;&lt; " and the last element is " &lt;&lt; v.back() &lt;&lt; endl;           
</pre>
		<p>This code produces the following output:</p>
		<pre class="example-code"> The first element is 0 and the last element is 4               
</pre>
		<p>
		</p>
		<hr />
		<p>
		</p>
		<div class="name-format">at </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; at( <strong>size_type</strong> loc );
  const <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; at( <strong>size_type</strong> loc ) const;
</pre>
		<p>The at() function<font color="#ff3300"> returns a reference to the element in the vector at index <em>loc</em></font>. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector.</p>
		<p>For example, consider the following code:</p>
		<pre class="example-code"> vector&lt;int&gt; v( 5, 1 );
 for( int i = 0; i &lt; 10; i++ ) {
   cout &lt;&lt; "Element " &lt;&lt; i &lt;&lt; " is " &lt;&lt; v[i] &lt;&lt; endl;
 }              
</pre>
		<p>This code overrunns the end of the vector, producing potentially dangerous results. The following code would be much safer:</p>
		<pre class="example-code"> vector&lt;int&gt; v( 5, 1 );
 for( int i = 0; i &lt; 10; i++ ) {
   cout &lt;&lt; "Element " &lt;&lt; i &lt;&lt; " is " &lt;&lt; v.at(i) &lt;&lt; endl;
 }              
</pre>
		<p>Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the vector and will throw an exception.</p>
		<div class="related-name-format">
				<hr />
		</div>
		<div class="name-format">capacity </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  <strong>size_type</strong> capacity() const;
</pre>
		<p>The capacity() function <font color="#ff3333">returns the number of elements that the vector can hold before it will need to allocate more space</font>.</p>
		<p>For example, the following code uses two different methods to set the capacity of two vectors. One method passes an argument to the constructor that suggests an initial size, the other method calls the reserve function to achieve a similar goal:</p>
		<pre class="example-code"> vector&lt;int&gt; v1(10);
 cout &lt;&lt; "The capacity of v1 is " &lt;&lt; v1.capacity() &lt;&lt; endl;
 vector&lt;int&gt; v2;
 v2.reserve(20);
 cout &lt;&lt; "The capacity of v2 is " &lt;&lt; v2.capacity() &lt;&lt; endl;         
</pre>
		<p>When run, the above code produces the following output:</p>
		<pre class="example-code"> The capacity of v1 is 10
 The capacity of v2 is 20               
</pre>
		<p>C++ containers are designed to grow in size dynamically. This frees the programmer from having to worry about storing an arbitrary number of elements in a container. However, sometimes the programmer can improve the performance of her program by giving hints to the compiler about the size of the containers that the program will use. These hints come in the form of the <a href="http://www.cppreference.com/cppvector/reserve.html">reserve</a>() function and the constructor used in the above example, which tell the compiler how large the container is expected to get.</p>
		<p>The capacity() function runs in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<div>
		</div>
		<hr />
		<div class="name-format">begin </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  iterator begin();
  const_iterator begin() const;
</pre>
		<p>The function begin() returns an iterator to the first element of the vector. begin() should run in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<p>For example, the following code uses begin() to initialize an iterator that is used to traverse a list:</p>
		<pre class="example-code">   // Create a list of characters
   list&lt;char&gt; charList;
   for( int i=0; i &lt; 10; i++ ) {
     charList.push_front( i + 65 );
   }
   // Display the list
   list&lt;char&gt;::iterator theIterator;
   for( theIterator = charList.begin(); theIterator != charList.end(); theIterator++ ) {
     cout &lt;&lt; *theIterator;
   }            
</pre>
		<div class="related-name-format">
				<hr />
		</div>
		<div class="name-format">max_size </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  <strong>size_type</strong> max_size() const;
</pre>
		<p>The max_size() function returns the maximum number of elements that the vector can hold. The max_size() function should not be confused with the <a href="http://www.cppreference.com/cppvector/size.html">size</a>() or <a href="http://www.cppreference.com/cppvector/capacity.html">capacity</a>() functions, which return the number of elements currently in the vector and the the number of elements that the vector will be able to hold before more memory will have to be allocated, respectively.</p>
		<div class="related-name-format">
				<hr />
		</div>
		<div class="name-format">clear </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  void clear();
</pre>
		<p>The function clear() deletes all of the elements in the vector. clear() runs in <a href="http://www.cppreference.com/complexity.html">linear time</a>.</p>
		<div class="related-name-format">
				<hr />
		</div>
		<div class="name-format">empty </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  bool empty() const;
</pre>
		<p>The empty() function returns true if the vector has no elements, false otherwise.</p>
		<p>For example, the following code uses empty() as the stopping condition on a (C/C++ Keywords) <a href="http://www.cppreference.com/keywords/while.html">while</a> loop to clear a vector and display its contents in reverse order:</p>
		<pre class="example-code"> vector&lt;int&gt; v;
 for( int i = 0; i &lt; 5; i++ ) {
   v.push_back(i);
 }
 while( !v.empty() ) {
   cout &lt;&lt; v.back() &lt;&lt; endl;
   v.pop_back();
 }              
</pre>
		<div class="related-name-format">
				<hr />
		</div>
		<div class="name-format">end </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  iterator end();
  const_iterator end() const;
</pre>
		<p>The end() function returns an iterator just past the end of the vector.</p>
		<p>Note that before you can access the last element of the vector using an iterator that you get from a call to end(), you'll have to decrement the iterator first. This is because end() doesn't point to the end of the vector; it points <strong>just past the end of the vector</strong>.</p>
		<p>For example, in the following code, the first "cout" statement will display garbage, whereas the second statement will actually display the last element of the vector:</p>
		<pre class="example-code">  vector&lt;int&gt; v1;
  v1.push_back( 0 );
  v1.push_back( 1 );
  v1.push_back( 2 );
  v1.push_back( 3 );

  int bad_val = *(v1.end());
  cout &lt;&lt; "bad_val is " &lt;&lt; bad_val &lt;&lt; endl;

  int good_val = *(v1.end() - 1);
  cout &lt;&lt; "good_val is " &lt;&lt; good_val &lt;&lt; endl;
</pre>
		<p>The next example shows how <a href="http://www.cppreference.com/cppvector/begin.html">begin</a>() and end() can be used to iterate through all of the members of a vector:</p>
		<pre class="example-code"> vector&lt;int&gt; v1( 5, 789
  ); vector&lt;int&gt;::iterator it; for( it = v1.begin(); it !=
  v1.end(); it++ ) { cout &lt;&lt; *it &lt;&lt; endl; } </pre>
		<p>The iterator is initialized with a call to <a href="http://www.cppreference.com/cppvector/begin.html">begin</a>(). After the body of the loop has been executed, the iterator is incremented and tested to see if it is equal to the result of calling end(). Since end() returns an iterator pointing to an element just after the last element of the vector, the loop will only stop once all of the elements of the vector have been displayed.</p>
		<p>end() runs in <a href="http://www.cppreference.com/complexity.html">constant time</a>.<br /><br /></p>
		<hr />
		<div class="name-format">erase </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  iterator erase( iterator loc );
  iterator erase( iterator start, iterator end );
</pre>
		<p>The erase() function either deletes the element at location <em>loc</em>, or deletes the elements between <em>start</em> and <em>end</em> (including <em>start</em> but not including <em>end</em>). The return value is the element after the last element erased.</p>
		<p>The first version of erase (the version that deletes a single element at location <em>loc</em>) runs in <a href="http://www.cppreference.com/complexity.html">constant time</a> for lists and <a href="http://www.cppreference.com/complexity.html">linear time</a> for vectors, dequeues, and strings. The multiple-element version of erase always takes <a href="http://www.cppreference.com/complexity.html">linear time</a>.</p>
		<p>For example:</p>
		<pre class="example-code"> // Create a vector, load it with the first ten characters of the alphabet
 vector&lt;char&gt; alphaVector;
 for( int i=0; i &lt; 10; i++ ) {
   alphaVector.push_back( i + 65 );
 }
 int size = alphaVector.size();
 vector&lt;char&gt;::iterator startIterator;
 vector&lt;char&gt;::iterator tempIterator;
 for( int i=0; i &lt; size; i++ ) {
   startIterator = alphaVector.begin();
   alphaVector.erase( startIterator );
   // Display the vector
   for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ ) {
     cout &lt;&lt; *tempIterator;
   }
   cout &lt;&lt; endl;
 }              
</pre>
		<p>That code would display the following output:</p>
		<pre class="example-code"> BCDEFGHIJ
 CDEFGHIJ
 DEFGHIJ
 EFGHIJ
 FGHIJ
 GHIJ
 HIJ
 IJ
 J              
</pre>
		<p>In the next example, erase() is called with two iterators to delete a range of elements from a vector:</p>
		<pre class="example-code"> // create a vector, load it with the first ten characters of the alphabet
 vector&lt;char&gt; alphaVector;
 for( int i=0; i &lt; 10; i++ ) {
   alphaVector.push_back( i + 65 );
 }
 // display the complete vector
 for( int i = 0; i &lt; alphaVector.size(); i++ ) {
   cout &lt;&lt; alphaVector[i];
 }
 cout &lt;&lt; endl;            

 // use erase to remove all but the first two and last three elements
 // of the vector
 alphaVector.erase( alphaVector.begin()+2, alphaVector.end()-3 );
 // display the modified vector
 for( int i = 0; i &lt; alphaVector.size(); i++ ) {
   cout &lt;&lt; alphaVector[i];
 }
 cout &lt;&lt; endl;            
</pre>
		<p>When run, the above code displays:</p>
		<pre class="example-code"> ABCDEFGHIJ
 ABHIJ          
</pre>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/clear.html">clear</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/insert.html">insert</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/pop_back.html">pop_back</a>
				<br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/pop_front.html">pop_front</a><br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/remove.html">remove</a><br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/remove_if.html">remove_if</a></div>
		<div>
		</div>
		<hr />
		<div class="name-format">front </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; front();
  const <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; front() const;
</pre>
		<p>The front() function returns a reference to the first element of the vector, and runs in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/back.html">back</a>
				<br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/pop_front.html">pop_front</a><br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/push_front.html">push_front</a></div>
		<div>
		</div>
		<hr />
		<div class="name-format">insert </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  iterator insert( iterator loc, const <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; val );
  void insert( iterator loc, <strong>size_type</strong> num, const <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; val );
  template&lt;<a href="http://www.cppreference.com/containers.html">TYPE</a>&gt; void insert( iterator loc, <a href="http://www.cppreference.com/iterators.html">input_iterator</a> start, <a href="http://www.cppreference.com/iterators.html">input_iterator</a> end );
</pre>
		<p>The insert() function either:</p>
		<ul>
				<li>inserts <em>val</em> before <em>loc</em>, returning an iterator to the element inserted, 
</li>
				<li>inserts <em>num</em> copies of <em>val</em> before <em>loc</em>, or 
</li>
				<li>inserts the elements from <em>start</em> to <em>end</em> before <em>loc</em>. </li>
		</ul>
		<p>Note that inserting elements into a vector can be relatively time-intensive, since the underlying data structure for a vector is an array. In order to insert data into an array, you might need to displace a lot of the elements of that array, and this can take <a href="http://www.cppreference.com/complexity.html">linear time</a>. If you are planning on doing a lot of insertions into your vector and you care about speed, you might be better off using a container that has a linked list as its underlying data structure (such as a <a href="http://www.cppreference.com/cpplist/index.html">List</a> or a <a href="http://www.cppreference.com/cppdeque/index.html">Deque</a>).</p>
		<p>For example, the following code uses the insert() function to splice four copies of the character 'C' into a vector of characters:</p>
		<pre class="example-code"> // Create a vector, load it with the first 10 characters of the alphabet
 vector&lt;char&gt; alphaVector;
 for( int i=0; i &lt; 10; i++ ) {
   alphaVector.push_back( i + 65 );
 }              

 // Insert four C's into the vector
 vector&lt;char&gt;::iterator theIterator = alphaVector.begin();
 alphaVector.insert( theIterator, 4, 'C' );             

 // Display the vector
 for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )    {
   cout &lt;&lt; *theIterator;
 }              
</pre>
		<p>This code would display:</p>
		<pre class="example-code"> CCCCABCDEFGHIJ         
</pre>
		<p>Here is another example of the insert() function. In this code, insert() is used to append the contents of one vector onto the end of another:</p>
		<pre class="example-code">  vector&lt;int&gt; v1;
  v1.push_back( 0 );
  v1.push_back( 1 );
  v1.push_back( 2 );
  v1.push_back( 3 );

  vector&lt;int&gt; v2;
  v2.push_back( 5 );
  v2.push_back( 6 );
  v2.push_back( 7 );
  v2.push_back( 8 );

  cout &lt;&lt; "Before, v2 is: ";
  for( int i = 0; i &lt; v2.size(); i++ ) {
    cout &lt;&lt; v2[i] &lt;&lt; " ";
  }
  cout &lt;&lt; endl;

  v2.insert( v2.end(), v1.begin(), v1.end() );

  cout &lt;&lt; "After, v2 is: ";
  for( int i = 0; i &lt; v2.size(); i++ ) {
    cout &lt;&lt; v2[i] &lt;&lt; " ";
  }
  cout &lt;&lt; endl;
</pre>
		<p>When run, this code displays:</p>
		<pre class="example-code">  Before, v2 is: 5 6 7 8
  After, v2 is: 5 6 7 8 0 1 2 3
</pre>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/assign.html">assign</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/erase.html">erase</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/push_back.html">push_back</a>
				<br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/merge.html">merge</a><br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/push_front.html">push_front</a><br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/splice.html">splice</a></div>
		<div>
		</div>
		<hr />
		<div class="name-format">Vector constructors </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  vector();
  vector( const vector&amp; c );
  vector( <strong>size_type</strong> num, const <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; val = <a href="http://www.cppreference.com/containers.html">TYPE</a>() );
  vector( <a href="http://www.cppreference.com/iterators.html">input_iterator</a> start, <a href="http://www.cppreference.com/iterators.html">input_iterator</a> end );
  ~vector();
</pre>
		<p>The default vector constructor takes no arguments, creates a new instance of that vector.</p>
		<p>The second constructor is a default copy constructor that can be used to create a new vector that is a copy of the given vector <em>c</em>.</p>
		<p>The third constructor creates a vector with space for <em>num</em> objects. If <em>val</em> is specified, each of those objects will be given that value. For example, the following code creates a vector consisting of five copies of the integer 42:</p>
		<pre class="example-code"> vector&lt;int&gt; v1( 5, 42 );         
</pre>
		<p>The last constructor creates a vector that is initialized to contain the elements between <em>start</em> and <em>end</em>. For example:</p>
		<pre class="example-code"> // create a vector of random integers
 cout &lt;&lt; "original vector: ";
 vector&lt;int&gt; v;
 for( int i = 0; i &lt; 10; i++ ) {
   int num = (int) rand() % 10;
   cout &lt;&lt; num &lt;&lt; " ";
   v.push_back( num );
 }
 cout &lt;&lt; endl;            

 // find the first element of v that is even
 vector&lt;int&gt;::iterator iter1 = v.begin();
 while( iter1 != v.end() &amp;&amp; *iter1 % 2 != 0 ) {
   iter1++;
 }              

 // find the last element of v that is even
 vector&lt;int&gt;::iterator iter2 = v.end();
 do {
   iter2--;
 } while( iter2 != v.begin() &amp;&amp; *iter2 % 2 != 0 );              

 cout &lt;&lt; "first even number: " &lt;&lt; *iter1 &lt;&lt; ", last even number: " &lt;&lt; *iter2 &lt;&lt; endl;         

 cout &lt;&lt; "new vector: ";
 vector&lt;int&gt; v2( iter1, iter2 );
 for( int i = 0; i &lt; v2.size(); i++ ) {
   cout &lt;&lt; v2[i] &lt;&lt; " ";
 }
 cout &lt;&lt; endl;            
</pre>
		<p>When run, this code displays the following output:</p>
		<pre class="example-code"> original vector: 1 9 7 9 2 7 2 1 9 8
 first even number: 2, last even number: 8
 new vector: 2 7 2 1 9          
</pre>
		<p>All of these constructors run in <a href="http://www.cppreference.com/complexity.html">linear time</a> except the first, which runs in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<p>The default destructor is called when the vector should be destroyed.</p>
		<div>
		</div>
		<hr />
		<div class="name-format">pop_back </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  void pop_back();
</pre>
		<p>The pop_back() function removes the last element of the vector.</p>
		<p>pop_back() runs in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/back.html">back</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/erase.html">erase</a>
				<br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/pop_front.html">pop_front</a><br /><a href="http://www.cppreference.com/cppvector/push_back.html">push_back</a></div>
		<div>
		</div>
		<hr />
		<div class="name-format">push_back </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  void push_back( const <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; val );
</pre>
		<p>The push_back() function appends <em>val</em> to the end of the vector.</p>
		<p>For example, the following code puts 10 integers into a list:</p>
		<pre class="example-code">   list&lt;int&gt; the_list;
   for( int i = 0; i &lt; 10; i++ )
     the_list.push_back( i );           
</pre>
		<p>When displayed, the resulting list would look like this:</p>
		<pre class="example-code"> 0 1 2 3 4 5 6 7 8 9            
</pre>
		<p>push_back() runs in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/assign.html">assign</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/insert.html">insert</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/pop_back.html">pop_back</a>
				<br />(C++ Lists) <a href="http://www.cppreference.com/cpplist/push_front.html">push_front</a></div>
		<div>
		</div>
		<hr />
		<div class="name-format">rbegin </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  <a href="http://www.cppreference.com/iterators.html">reverse_iterator</a> rbegin();
  const_<a href="http://www.cppreference.com/iterators.html">reverse_iterator</a> rbegin() const;
</pre>
		<p>The rbegin() function returns a <a href="http://www.cppreference.com/iterators.html">reverse_iterator</a> to the end of the current vector.</p>
		<p>rbegin() runs in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/begin.html">begin</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/end.html">end</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/rend.html">rend</a>
		</div>
		<div>
		</div>
		<hr />
		<div class="name-format">rend </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  <a href="http://www.cppreference.com/iterators.html">reverse_iterator</a> rend();
  const_<a href="http://www.cppreference.com/iterators.html">reverse_iterator</a> rend() const;
</pre>
		<p>The function rend() returns a <a href="http://www.cppreference.com/iterators.html">reverse_iterator</a> to the beginning of the current vector.</p>
		<p>rend() runs in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/begin.html">begin</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/end.html">end</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/rbegin.html">rbegin</a>
		</div>
		<div>
		</div>
		<hr />
		<div class="name-format">reserve </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  void reserve( <strong>size_type</strong> size );
</pre>
		<p>The reserve() function sets the capacity of the vector to at least <em>size</em>.</p>
		<p>reserve() runs in <a href="http://www.cppreference.com/complexity.html">linear time</a>.</p>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/capacity.html">capacity</a>
		</div>
		<div>
		</div>
		<hr />
		<div class="name-format">resize </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  void resize( <strong>size_type</strong> num, const <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; val = <a href="http://www.cppreference.com/containers.html">TYPE</a>() );
</pre>
		<p>The function resize() changes the size of the vector to <em>size</em>. If <em>val</em> is specified then any newly-created elements will be initialized to have a value of <em>val</em>.</p>
		<p>This function runs in <a href="http://www.cppreference.com/complexity.html">linear time</a>.</p>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/vector_constructors.html">Vector constructors &amp; destructors</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/capacity.html">capacity</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/size.html">size</a>
		</div>
		<div>
		</div>
		<hr />
		<div class="name-format">size </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  <strong>size_type</strong> size() const;
</pre>
		<p>The size() function returns the number of elements in the current vector.</p>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/capacity.html">capacity</a>
				<br />
				<a href="http://www.cppreference.com/cppvector/empty.html">empty</a>
				<br />(C++ Strings) <a href="http://www.cppreference.com/cppstring/length.html">length</a><br /><a href="http://www.cppreference.com/cppvector/max_size.html">max_size</a><br /><a href="http://www.cppreference.com/cppvector/resize.html">resize</a></div>
		<div>
		</div>
		<hr />
		<div class="name-format">swap </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  void swap( const container&amp; from );
</pre>
		<p>The swap() function exchanges the elements of the current vector with those of <em>from</em>. This function operates in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<p>For example, the following code uses the swap() function to exchange the values of two strings:</p>
		<pre class="example-code">   string first( "This comes first" );
   string second( "And this is second" );
   first.swap( second );
   cout &lt;&lt; first &lt;&lt; endl;
   cout &lt;&lt; second &lt;&lt; endl;          
</pre>
		<p>The above code displays:</p>
		<pre class="example-code">   And this is second
   This comes first             
</pre>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">(C++ Lists) <a href="http://www.cppreference.com/cpplist/splice.html">splice</a></div>
		<div>
		</div>
		<hr />
		<div class="name-format">Vector operators </div>
		<div class="syntax-name-format">Syntax: </div>
		<pre class="syntax-box">  #include &lt;vector&gt;
  <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; operator[]( <strong>size_type</strong> index );
  const <a href="http://www.cppreference.com/containers.html">TYPE</a>&amp; operator[]( <strong>size_type</strong> index ) const;
  vector operator=(const vector&amp; c2);
  bool operator==(const vector&amp; c1, const vector&amp; c2);
  bool operator!=(const vector&amp; c1, const vector&amp; c2);
  bool operator&lt;(const vector&amp; c1, const vector&amp; c2);
  bool operator&gt;(const vector&amp; c1, const vector&amp; c2);
  bool operator&lt;=(const vector&amp; c1, const vector&amp; c2);
  bool operator&gt;=(const vector&amp; c1, const vector&amp; c2);
</pre>
		<p>All of the C++ containers can be compared and assigned with the standard comparison operators: ==, !=, &lt;=, &gt;=, &lt;, &gt;, and =. Individual elements of a vector can be examined with the [] operator.</p>
		<p>Performing a comparison or assigning one vector to another takes <a href="http://www.cppreference.com/complexity.html">linear time</a>. The [] operator runs in <a href="http://www.cppreference.com/complexity.html">constant time</a>.</p>
		<p>Two vectors are equal if:</p>
		<ol>
				<li>Their size is the same, and 
</li>
				<li>Each member in location i in one vector is equal to the the member in location i in the other vector. </li>
		</ol>
		<p>Comparisons among vectors are done lexicographically.</p>
		<p>For example, the following code uses the [] operator to access all of the elements of a vector:</p>
		<pre class="example-code"> vector&lt;int&gt; v( 5, 1 );
 for( int i = 0; i &lt; v.size(); i++ ) {
   cout &lt;&lt; "Element " &lt;&lt; i &lt;&lt; " is " &lt;&lt; v[i] &lt;&lt; endl;
 }              
</pre>
		<div class="related-name-format">Related topics: </div>
		<div class="related-content">
				<a href="http://www.cppreference.com/cppvector/at.html">at</a>
		</div>
		<div>
		</div>
		<hr />
<img src ="http://www.cppblog.com/keyws/aggbug/11194.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-13 19:52 <a href="http://www.cppblog.com/keyws/archive/2006/08/13/11194.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转】vector（一）</title><link>http://www.cppblog.com/keyws/archive/2006/08/13/11193.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Sun, 13 Aug 2006 11:36:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/13/11193.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11193.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/13/11193.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11193.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11193.html</trackback:ping><description><![CDATA[
		<p align="left">
				<strong>(一)<br /></strong>
				<br />One of the basic classes implemented by the Standard Template Library is the vector class. A vector is, essentially, a resizeable array; the vector class allows random access via the [] operator, but adding an element anywhere but to the end of a vector causes some overhead as all of the elements are shuffled around to fit them correctly into memory. Fortunately, the memory requirements are equivalent to those of a normal array. The header file for the STL vector library is vector. (Note that when using C++, header files drop the .h; for C header files - e.g. stdlib.h - you should still include the .h.) Moreover, the vector class is part of the std <a href="http://www.cprogramming.com/tutorial/namespaces.html"><font color="#c30000">namespace</font></a>, so you must either prefix all references to the vector template with std:: or include "using namespace std;" at the top of your program.</p>
		<p>Vectors are more powerful than arrays because the number of functions that are available for accessing and modifying vectors. Unfortunately, the [] operator still does not provide bounds checking. There is an alternative way of accessing the vector, using the function at, which does provide bounds checking at an additional cost. Let's take a look at several functions provided by the vector class:<br /><br /></p>
		<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: #000000">unsigned </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> size(); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Returns the number of elements in a vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">push_back(type element); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Adds an element to the end of a vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">3</span> <span style="COLOR: #008000"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> empty(); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Returns true if the vector is empty</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">4</span> <span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> clear(); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Erases all elements of the vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">5</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">type at(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Returns the element at index n, with bounds checking</span></div>
		<p>also, there are several basic operators defined for the vector class:</p>
		<pre class="example">=           Assignment replaces a vector's contents with the contents of another
==          An element by element comparison of two vectors
[]          Random access to an element of a vector (usage is similar to that
            of the operator with arrays.) Keep in mind that it does not provide
            bounds checking.</pre>
		<p>Let's take a look at an example program using the vector class:</p>
		<pre class="example">
				<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: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main()<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">    vector </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> example;         </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Vector to store integers</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    example.push_back(</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">);         </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Add 3  onto the vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    example.push_back(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Add 10 to the end</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    example.push_back(</span><span style="COLOR: #000000">33</span><span style="COLOR: #000000">);        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Add 33 to the end<br /></span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #008000"></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"> x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; x</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">example.size(); x</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">) <br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">        cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">example[x]</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">;    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Should output: 3 10 33</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    }<br /><br /></span><span style="COLOR: #008080">15</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">example.empty())          </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Checks if empty</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">        example.clear();          </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Clears vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    vector </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> another_vector;  </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Creates another vector to store integers</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    another_vector.push_back(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Adds to end of vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    example.push_back(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Same</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(example</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">another_vector)   </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">To show testing equality</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">        example.push_back(</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">); <br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">24</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"> y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; y</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">example.size(); y</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">        cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">example[y]</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">;    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Should output 10 20</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">}</span></div>
		</pre>
		<b>Summary of Vector Benefits</b>
		<p>Vectors are somewhat easier to use than regular arrays. At the very least, they get around having to be resized constantly using new and delete. Furthermore, their immense flexibility - support for any datatype and support for automatic resizing when adding elements - and the other helpful included functions give them clear advantages to arrays.</p>
		<p align="left">Another argument for using vectors are that they help avoid memory leaks--you don't have to remember to free vectors, or worry about how to handle freeing a vector in the case of an exception. This simplifies program flow and helps you write tighter code. Finally, if you use the at() function to access the vector, you get bounds checking at the cost of a slight performance penalty.<br /><br /><strong>(二)</strong><br /><br /> C++ <a class="kLink" oncontextmenu="return false;" id="KonaLink0" onmouseover="adlinkMouseOver(event,this,0);" style="POSITION: relative; TEXT-DECORATION: underline! important" onclick="adlinkMouseClick(event,this,0);" onmouseout="adlinkMouseOut(event,this,0);" href="http://www.codersource.net/c++_vector_stl.html#" target="_top"><font style="FONT-WEIGHT: 400; COLOR: blue; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: relative" color="blue" size="2"><span class="kLink" style="FONT-WEIGHT: 400; COLOR: blue; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: relative">vector</span></font></a> is a container template available with Standard Template Library pack. This C++ vector can be used to store similar typed objects sequentially, which can be accessed at a later point of time. As this is a template, all types of data including user defined data types like struct and class can be stored in this container. </p>
		<p>This article explains briefly about how to insert, delete, access the data with respect to the C++ vector. The type stl::string is used for the purposes of explaining the sample code. Using stl::string is only for sample purposes. Any other type can be used with the C++ vector. <br /><br />The values can be added to the c++ vector at the end of the sequence. The function push_back should be used for this purpose. The &lt;vector&gt; header file should be included in all the header files in order to access the C++ vector and its functions.<br /></p>
		<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: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</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"> 4</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> main()<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Declaration for the string data</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> strData </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">One</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Declaration for C++ vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    std:: vector </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> str_Vector;<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">    str_Vector.push_back(strData);<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">    strData </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Two</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">    str_Vector.push_back(strData);<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    strData </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Three</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">    str_Vector.push_back(strData);<br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">    strData </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Four</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">    str_Vector.push_back(strData);<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">}</span></div>
		<p align="left">The above code adds 4 strings of std::string type to the str_Vector. This uses std:: vector.push_back function. This function takes 1 parameter of the designated type and adds it to the end of the c++ vector.</p>
		<h2>Accessing Elements of C++ Vector:</h2>
		<p>   The elements after being added can be accessed in two ways. One way is our legacy way of accessing the data with vector.at(position_in_integer) function. The position of the data element is passed as the single parameter to access the data.</p>
		<h3>Using our normal logic for accessing elements stored in C++ Vector:</h3>
		<p>If we want to access all the data, we can use a for loop and display them as follows.<br /><br /></p>
		<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: #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">0</span><span style="COLOR: #000000">;i </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"> str_Vector.size(); i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">3</span> <span style="COLOR: #000000">    std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> strd </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> str_Vector.at(i);<br /></span><span style="COLOR: #008080">4</span> <span style="COLOR: #000000">    cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">strd.c_str()</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br /></span><span style="COLOR: #008080">5</span> <span style="COLOR: #000000">}</span></div>
		<p>The std:: vector .size() function returns the number of elements stored in the vector.</p>
		<h3>Using C++ vector iterator provided by STL:</h3>
		<p>The next way is to use the iterator object provided by the STL. These iterators are general purpose pointers allowing c++ programmers to forget the intricacies of object types and access the data of any type.<br /><br /></p>
		<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: #000000">std::vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator itVectorData;<br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(itVectorData </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> str_Vector.begin();  itVectorData </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> str_Vector.end(); itVectorData</span><span style="COLOR: #000000">++</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">    std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> strD </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(itVectorData);<br /></span><span style="COLOR: #008080">5</span> <span style="COLOR: #000000">}</span></div>
		<h2>Removing elements from C++ vector:</h2>
		<p>Removing elements one by one from specified positions in c++ vector is achieved with the erase function. </p>
		<p>The following code demonstrates how to use the erase function to remove an element from position 2 in the str_Vector from our sample.</p>
		<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>
				<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">str_Vector.erase(str_Vector.begin()</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">,str_Vector.begin()</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000">);</span>
		</div>
		<p> The following sample demonstrates how to use the erase() function for removing elements 2,3 in the str_Vector used in the above sample. </p>
		<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>
				<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">str_Vector.erase(str_Vector.begin()</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">,str_Vector.begin()</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">3</span>
				<span style="COLOR: #000000">);</span>
		</div>
		<p>If one wants to remove all the elements at once from the c++ vector, the vector.clear() function can be used.</p>
		<!-- End of text -->
<img src ="http://www.cppblog.com/keyws/aggbug/11193.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-13 19:36 <a href="http://www.cppblog.com/keyws/archive/2006/08/13/11193.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转】iterators</title><link>http://www.cppblog.com/keyws/archive/2006/08/13/11187.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Sun, 13 Aug 2006 11:04:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/13/11187.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11187.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/13/11187.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11187.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11187.html</trackback:ping><description><![CDATA[
		<h1>
				<a href="http://www.cprogramming.com/tutorial/stl/iterators.html">
						<font size="3">http://www.cprogramming.com/tutorial/stl/iterators.html</font>
				</a>
		</h1>The concept of an iterator is fundamental to understanding the C++ Standard Template Library (STL) because iterators provide a means for accessing data stored in container classes such a <a href="http://www.cprogramming.com/tutorial/stl/vector.html">vector</a>, map, list, etc. <br /><br />You can think of an iterator as pointing to an item that is part of a larger container of items. For intance, all containers support a function called begin, which will return an iterator pointing to the beginning of the container (the first element) and function, end, that returns an iterator corresponding to having reached the end of the container. In fact, you can access the element by "dereferencing" the iterator with a *, just as you would dereference a pointer. <br /><br />To request an iterator appropriate for a particular STL templated class, you use the syntax <br /><br /><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: #000000">std::class_name</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">template_parameters</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator name</span></div><br />where <i>name</i> is the name of the iterator variable you wish to create and the <i>class_name</i> is the name of the STL container you are using, and the <i>template_paramters</i> are the parameters to the template used to declare objects that will work with this iterator. Note that because the STL classes are part of the std <a href="http://www.cprogramming.com/tutorial/namespaces.html">namespace</a>, you will need to either prefix every container class type with "std::", as in the example, or include "using namespace std;" at the top of your program. <br /><br />For instance, if you had an STL vector storing integers, you could create an iterator for it as follows: <pre class="example"><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><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">std::vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> myIntVector;<br /></span><span style="COLOR: #008080">2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />std::vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator myIntVectorIterator;</span></div></pre>Different operations and containers support different types of iterator behavior. In fact, there are several different classes of iterators, each with slightly different properties. First, iterators are distinguished by whether you can use them for reading or writing data in the container. Some types of iterators allow for both reading and writing behavior, though not necessarily at the same time. <br /><br />Some of the most important are the forward, backward and the bidirectional iterators. Both of these iterators can be used as either input or output iterators, meaning you can use them for either writing or reading. The forward iterator only allows movement one way -- from the front of the container to the back. To move from one element to the next, the increment operator, ++, can be used. <br /><br />For instance, <font color="#999933">if you want to access the elements of an STL vector, it's best to use an iterator instead of the traditional C-style code.</font> The strategy is fairly straightforward: call the container's begin function to get an iterator, use ++ to step through the objects in the container, access each object with the * operator ("*iterator") similar to the way you would access an object by dereferencing a pointer, and stop iterating when the iterator equals the container's end iterator. You can compare iterators using != to check for inequality, == to check for equality. (This only works for one twhen the iterators are operating on the same container!) <br /><br /><b>The old approach (avoid)</b><pre class="example"><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: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> myIntVector;<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Add some elements to myIntVector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">myIntVector.push_back(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">myIntVector.push_back(</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">myIntVector.push_back(</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">10</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"> y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; y</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">myIntVector.size(); y</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">    cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">myIntVector[y]</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">;  </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Should output 1 4 8</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">}</span></div></pre><b>The STL approach (use this)</b><pre class="example"><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: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> myIntVector;<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator myIntVectorIterator;<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Add some elements to myIntVector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">myIntVector.push_back(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">myIntVector.push_back(</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">myIntVector.push_back(</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(myIntVectorIterator </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> myIntVector.begin(); <br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">        myIntVectorIterator </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> myIntVector.end();<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">        myIntVectorIterator</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">    cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">myIntVectorIterator</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">;    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Should output 1 4 8</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></div></pre>As you might imagine, you can use the decrement operator, --, when working with a bidirectional iterator or a backward operator. <br /><br />Iterators are often handy for specifying a particular range of things to operate on. For instance, the range <i>item</i>.begin(), <i>item</i>.end() is the entire container, but smaller slices can be used. This is particularly easy with one other, extremely general class of iterator, the random access iterator, which is functionally equivalent to a pointer in C or C++ in the sense that you can not only increment or decrement but also move an arbitrary distance in constant time (for instance, jump multiple elements down a vector). <br /><br />For instance, the iterators associated with vectors are random access iterators so you could use arithmetic of the form <pre class="example"><i>iterator</i> + n
</pre>where n is an integer. The result will be the element corresponding to the nth item after the item pointed to be the current iterator. This can be a problem if you happen to exceed the bounds of your iterator by stepping forward (or backward) by too many elements. <br /><br />The following code demonstrates both the use of random access iterators and exceeding the bounds of the array (don't run it!): <pre class="example"><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: #000000">vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> myIntVector;<br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator myIntVectorIterator;<br /></span><span style="COLOR: #008080">3</span> <span style="COLOR: #000000">myIntVectorIterator </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> myIntVector.begin() </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;</span></div></pre>You can also use the standard arithmetic shortcuts for addition and subtraction, += and -=, with random access iterators. Moreover, <font color="#339966">with random access iterators you can use &lt;, &gt;, &lt;=, and &gt;= to compare iterator positions within the container</font>. <br /><br />Iterators are also useful for some functions that belong to container classes that require operating on a range of values. A simple but useful example is the erase function. The vector template supports this function, which takes a range as specified by two iterators -- every element in the range is erased. For instance, to erase an entire vector: <pre class="example"><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: #000000">vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator myIntVectorIterator;<br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000">myIntVector.erase(myIntVectorIterator.begin(), myIntVectorIterator.end());</span></div></pre>which would delete all elements in the vector. If you only wanted to delete the first two elements, you could use <pre class="example"><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><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">myIntVector.erase(myIntVectorIterator.begin(), myIntVectorIterator.begin()</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">);</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span></div></pre>Note that various container class support different types of iterators -- the vector class, which has served as our model for iterators, supports a random access iterator, the most general kind. Another container, the list container (to be discussed later), only supports bidirectional iterators. <br /><br />So why use iterators? First, they're a flexible way to access the data in containers that don't have obvious means of accessing all of the data (for instance, maps [to be discussed later]). They're also quite flexible -- if you change the underlying container, it's easy to change the associated iterator so long as you only use features associated with the iterator supported by both classes. Finally, the STL algorithms defined in &lt;algorithm&gt; (to be discussed later) use iterators. 
<h4>Summary</h4><b>The Good</b><ul><li>The STL provides iterators as a convenient abstraction for accessing many different types of containers. 
</li><li>Iterators for templated classes are generated inside the class scope with the syntax <pre class="example"><i>class_name</i>&lt;<i>parameters</i>&gt;::iterator
</pre></li><li>Iterators can be thought of as limited pointers (or, in the case of random access iterators, as nearly equivalent to pointers) </li></ul><b>The Gotchas</b><ul><li>Iterators do not provide bounds checking; it is possible to overstep the bounds of a container, resulting in segmentation faults 
</li><li>Different containers support different iterators, so it is not always possible to change the underlying container type without making changes to your code 
</li><li>Iterators can be invalidated if the underlying container (the container being iterated over) is changed significantly </li></ul><img src ="http://www.cppblog.com/keyws/aggbug/11187.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-13 19:04 <a href="http://www.cppblog.com/keyws/archive/2006/08/13/11187.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【原】一个关于函数模版编译错误的调试记录</title><link>http://www.cppblog.com/keyws/archive/2006/08/12/11154.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Sat, 12 Aug 2006 12:32:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/12/11154.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11154.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/12/11154.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11154.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11154.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">初学函数模版，一个“</span>
				<span lang="EN-US">bug”</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">让俺郁闷了一番，想了一个小时都没有想出所以然。虽求助于网络，发贴于</span>
				<span lang="EN-US">vckbase</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">问题已经解决，帖子摘要如下：<br /><br /></span>
		</p>
		<table class="MsoNormalTable" style="WIDTH: 100%; mso-cellspacing: 0cm; mso-padding-alt: 3.75pt 3.75pt 3.75pt 3.75pt" cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes">
								<td style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 3.75pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 3.75pt; BACKGROUND: whitesmoke; PADDING-BOTTOM: 3.75pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 3.75pt; BORDER-BOTTOM: #ece9d8">
										<p class="MsoNormal" style="MARGIN: 0cm 0cm 12pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
												<span style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">主<span lang="EN-US">      </span>题<span lang="EN-US">: </span>函数模版问题，为什么不能采用多文件方式。<span lang="EN-US"> <?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /?><v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape id="_x0000_i1025" style="WIDTH: 11.25pt; HEIGHT: 11.25pt" alt="回复根帖" type="#_x0000_t75"><v:imagedata o:href="http://www.vckbase.com/bbs/image/rp2.gif" src="file:///C:\DOCUME~1\public\LOCALS~1\Temp\msohtml1\01\clip_image001.gif"></v:imagedata></v:shape><v:shape id="_x0000_i1026" style="WIDTH: 7.5pt; HEIGHT: 7.5pt" alt="" type="#_x0000_t75"><v:imagedata o:href="http://www.vckbase.com/bbs/image/rf.gif" src="file:///C:\DOCUME~1\public\LOCALS~1\Temp\msohtml1\01\clip_image002.gif"></v:imagedata></v:shape><br /></span>作<span lang="EN-US">      </span>者<span lang="EN-US">: <a href="http://www.vckbase.com/bbs/userinfo.asp?id=%B9%F9%B3%BF" target="_blank"><span lang="EN-US"><span lang="EN-US">郭晨</span></span></a> (
<script><![CDATA[
MyLevel(0)
  ]]&gt;</script></span>书童<span lang="EN-US">)<br /></span>所属论坛<span lang="EN-US">: C++ </span>论坛<span lang="EN-US"><br /></span>本帖分数<span lang="EN-US">: 0<br /></span>回复次数<span lang="EN-US">: 4<br /></span>发表时间<span lang="EN-US">: 2006-8-12 19:09:50<br /></span>正文内容<span lang="EN-US">:<br />1</span>）<span lang="EN-US"><br />#include &lt;fstream&gt;<br />#include &lt;iostream&gt;<br />using namespace std;<br />template&lt;class Type &gt;<br />Type min(Type a, Type b)<br />{<br />    return a &lt; b ? a : b;<br />}<br /><br />int main(int argc, char* argv[])<br />{<br />    cout &lt;&lt; min(10, 20) &lt;&lt; endl;<br />    return 0;<br />}<br /><br /></span>可以编译成功，运行得到正确结果。<span lang="EN-US"><br /><br />2</span>）多文件方式<span lang="EN-US"><br /><br />///////////////////////main.cxx<br />#include "min.h"<br />#include &lt;fstream&gt;<br />#include &lt;iostream&gt;<br />using namespace std;<br />int main(int argc, char* argv[])<br />{<br />    cout &lt;&lt; min(10, 20) &lt;&lt; endl;<br />    return 0;<br />}<br />///////////////////////min.h<br />#ifndef _MIN_GHH_<br />#define _MIN_GHH_  1<br /><br />template&lt;class Type &gt;<br />Type min(Type a, Type b);<br /><br />#endif<br /><br />//////////////////////min.cxx<br />#include "min.h"<br /><br />template&lt;class Type &gt;<br />Type min(Type a, Type b)<br />{<br />    return a &lt; b ? a : b;<br />}<br /><br /></span>编译报告错误：<span lang="EN-US"><br />Linking...<br />20060812_function_template.obj : error LNK2001: unresolved external symbol "int __cdecl min(int,int)" (?min@@YAHHH@Z)<br />Debug/20060812_function_template.exe : fatal error LNK1120: 1 unresolved externals<br />Error executing link.exe.<br /><br />20060812_function_template.exe - 2 error(s), 0 warning(s)<br /><br />////////////////////////////////////<br /></span>问题：为什么会出现这种问题，如何解决？盼赐教。<span lang="EN-US"><br /></span>最新修改<span lang="EN-US">:2006-8-12 19:41:10<o:p></o:p></span></span>
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table class="MsoNormalTable" style="WIDTH: 100%; mso-cellspacing: 0cm; mso-padding-alt: 0cm 0cm 0cm 0cm" cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes">
								<td style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 0cm; BORDER-TOP: #ece9d8; PADDING-LEFT: 0cm; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ece9d8; PADDING-TOP: 0cm; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent">
										<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
												<span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
														<o:p> </o:p>
												</span>
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<o:p>
						</o:p>
				</span>
		</p>
		<table class="MsoNormalTable" style="WIDTH: 100%; mso-cellspacing: 0cm; mso-padding-alt: 0cm 0cm 0cm 0cm" cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes">
								<td style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 0cm; BORDER-TOP: #ece9d8; PADDING-LEFT: 0cm; BACKGROUND: white; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ece9d8; WIDTH: 12.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: #ece9d8" width="17">
										<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
												<span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
														<o:p>
														</o:p>
												</span>
										</p>
								</td>
								<td style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 0cm; BORDER-TOP: #ece9d8; PADDING-LEFT: 0cm; BACKGROUND: #d0e6d0; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ece9d8; PADDING-TOP: 0cm; BORDER-BOTTOM: #ece9d8">
										<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
												<span style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">回复人<span lang="EN-US">: <a href="http://www.vckbase.com/bbs/userinfo.asp?id=newgun" target="_blank">newgun</a> (
<script><![CDATA[
MyLevel(5)
  ]]&gt;</script></span>书童<span lang="EN-US">) <v:shape id="_x0000_i1030" style="WIDTH: 11.25pt; HEIGHT: 11.25pt" alt="回复子帖" type="#_x0000_t75"><v:imagedata o:href="http://www.vckbase.com/bbs/image/rp2.gif" src="file:///C:\DOCUME~1\public\LOCALS~1\Temp\msohtml1\01\clip_image001.gif"></v:imagedata></v:shape><o:p></o:p></span></span>
										</p>
								</td>
								<td style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 0cm; BORDER-TOP: #ece9d8; PADDING-LEFT: 0cm; BACKGROUND: #d0e6d0; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ece9d8; PADDING-TOP: 0cm; BORDER-BOTTOM: #ece9d8">
										<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: right; mso-pagination: widow-orphan" align="right">
												<span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">2006-8-12 19:34:29 (</span>
												<span style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">得分<span lang="EN-US">:</span></span>
												<span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">10</span>
												<span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">) <o:p></o:p></span>
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">Re:</span>
				<span style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">函数模版问题，为什么不能采用多文件方式。<span lang="EN-US"><br /></span>在<span lang="EN-US">c++</span>标准中规定可以采用分离的编译模式，只需要通过在模板定义中的关键字<span lang="EN-US">template </span>之前加上关键字<span lang="EN-US">export </span>来声明一个可导出的函数模板当函数模板，被导出时我们就可以在任意程序文本文件中使用模板的实例如：<span lang="EN-US"><br />  // model2.h<br />// </span>分离模式<span lang="EN-US">: </span>只提供模板声明<span lang="EN-US"><br />template &lt;typename Type&gt; Type min( Type t1, Type t2 );<br />// model2.C<br />// the template definition<br />export template &lt;typename Type&gt;    //export</span>关键字<span lang="EN-US"><br />Type min( Type t1, Type t2 ) { /* ...*/ }<br /><br />    </span><font color="#669933">但是好像现在的好多编译器如<span lang="EN-US">vc7</span>都还不支持这种结构，所以最好把模板的声明</font><span lang="EN-US"><br /></span><font color="#669933">和定义都放在头文件里</font>。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<div style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 0cm; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; PADDING-TOP: 1pt; BORDER-BOTTOM: medium none; mso-element: para-border-div; mso-border-top-alt: solid windowtext .75pt">
				<p class="MsoNormal" style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 0cm; MARGIN: 0cm 0cm 0pt; BORDER-LEFT: medium none; PADDING-TOP: 0cm; BORDER-BOTTOM: medium none; TEXT-ALIGN: center; mso-pagination: widow-orphan; mso-padding-alt: 1.0pt 0cm 0cm 0cm; mso-border-top-alt: solid windowtext .75pt" align="center">
				</p>
				<p class="MsoNormal" style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 0cm; MARGIN: 0cm 0cm 0pt; BORDER-LEFT: medium none; PADDING-TOP: 0cm; BORDER-BOTTOM: medium none; TEXT-ALIGN: center; mso-pagination: widow-orphan; mso-padding-alt: 1.0pt 0cm 0cm 0cm; mso-border-top-alt: solid windowtext .75pt" align="center">
						<span style="DISPLAY: none; FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial; mso-hide: all">
						</span>
						<span lang="EN-US" style="DISPLAY: none; FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt; mso-hide: all">
								<o:p>
								</o:p>
						</span>
				</p>
				<span lang="EN-US">
						<span style="mso-tab-count: 1">      </span>
				</span>
				<p>
				</p>
		</div>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">感受：得到网友提示，真有一种“柳暗花明又一村”之感，这个所谓</span>
				<span lang="EN-US">bug</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是这么简单又是这么不简单！</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一直认为应该是自己的错误，但没想到是编译器和标准兼容性的问题。看来自己的思维习惯应该有所改变了。应该多角度分析问题，而不能一味的想当然。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>Ps</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：自己也应该考虑尝试一个新的</span>
				<span lang="EN-US">C++</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编译器了，老用</span>
				<span lang="EN-US">vc</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，感觉学的标准</span>
				<span lang="EN-US">C++</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">都不纯，就象这次事件一样。</span>
		</p>
<img src ="http://www.cppblog.com/keyws/aggbug/11154.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-12 20:32 <a href="http://www.cppblog.com/keyws/archive/2006/08/12/11154.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【原】往事不可谏，来者犹可追！</title><link>http://www.cppblog.com/keyws/archive/2006/08/11/11143.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Fri, 11 Aug 2006 14:28:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/11/11143.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11143.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/11/11143.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11143.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11143.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">屈指一数，两年小硕，已过算半。回忆研一，展望研二，心中颇多感慨，喜忧四七开。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">喜，相比入学之初，知识层虽无脱胎换骨之感，但也小有所学，比已比人，进步不少！</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">忧，两年小硕，时间紧凑，于学术研究难有大成；再现实一点，虽学通信，但找一满意工作，仍压力不轻，任重道远！</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">读研不似本科，成熟多了，也开始考虑现实，不敢虚浮度日，但真努力奋斗起来，仍有迷茫不知所为之惑。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不过，还好，还有一颗虽笨拙但还善于反思的脑袋，虽弯路不少，但也算一步步的向前行了。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">两年，真的太短！何况已浪费一年光阴哉</span>
				<span lang="EN-US">?!</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">无论为了工作，为了毕业，还是为了自己做点成果来，都是该努力的时候了！</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不谈人生意义等等大道理，就是为了追求一种生存的基础，在一只脚已经踏入社会的时候，也该明白自己应该做些什么了！</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">管理自己，做该做的事，做一个积极的自己！完善自我！努力！<img height="20" src="http://www.cppblog.com/Emoticons/QQ/45.gif" width="20" border="0" /></span> </p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
						<o:p> </o:p>
				</span>
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>Ps</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：谨以此文，做开博纪念，也为我意义上的研二开始的纪念。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"> </p>
<img src ="http://www.cppblog.com/keyws/aggbug/11143.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-11 22:28 <a href="http://www.cppblog.com/keyws/archive/2006/08/11/11143.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>