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

📄 6372.htm

📁 C++细节解释
💻 HTM
字号:
<HTML>
<HEAD>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>


<style >
.fst{padding:0px 15px;width:770px;border-left:0px solid #000000;border-right:0px solid #000000}
.fstdiv3 img{border:0px;border-right:8px solid #eeeecc;border-top:6px solid #eeeecc}
</style>
<title>
Guru of the week:#18 迭代指针.
</title>
</HEAD>
<BODY>
<center>

<div align=center><div class=fst align=left><div class=fstdiv3 id=print2>
<font color="#000000" size="4"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Guru of the week:#18 迭代指针.</b></font><P>/*此文是译者出于自娱翻译的GotW(Guru of the Week)系列文章的一篇,原文的版权是属于Hub Sutter(著名的C++专家,《Exceptional C++》的作者)。此文的翻译没有征得原作者的同意,只供学习讨论。——译者:黄森堂*/</P>  
<P><FONT color=#0000ff size=4><B>#18 迭代指针.</B></FONT></P>  
<P><B>难度:</B>7/10</P>  
<P>任何程序在使用标准库都知道使用公共与非公共iterator容易犯错误,你发现了多少种呢?</P>  
<P><B>问题:</B></P>  
<P>以下程序至少有四处关于iterator的问题,你发现多少呢?</P><PRE>    int main( int, char*[] ) {
      vector&lt;Date&gt; e;
      copy( istream_iterator&lt;Date&gt;( cin ),
            istream_iterator&lt;Date&gt;(),
            back_inserter( e ) );

      vector&lt;Date&gt;::iterator first =
            find( e.begin(), e.end(), &quot;01/01/95&quot; );
      vector&lt;Date&gt;::iterator last =
            find( e.begin(), e.end(), &quot;12/31/95&quot; );

      *last = &quot;12/30/95&quot;;

      copy( first, last,
            ostream_iterator&lt;Date&gt;( cout, &quot;\n&quot; ) );

      e.insert( --e.end(), TodaysDate() );

      copy( first, last,
            ostream_iterator&lt;Date&gt;( cout, &quot;\n&quot; ) );
    }</PRE>  
<P><B>解决方法:</B></P>  
<P><FONT color=#999933>以下程序至少有四处关于iterator的问题,你发现多少</FONT><FONT color=#999933>呢</FONT><FONT color=#999933>?</FONT></P><PRE><FONT color=#999933>    int main( int, char*[] ) {
      vector&lt;Date&gt; e;
      copy( istream_iterator&lt;Date&gt;( cin ),
            istream_iterator&lt;Date&gt;(),
            back_inserter( e ) );</FONT></PRE>  
<P>到这儿是好的,Date类重载了运算符&gt;&gt;( istream&amp;, Date&amp; )来提供抽取功能,Date类用istream_iterator&lt;Date&gt;从cin流中读取Date数据,copy算法仅仅填充Date数据到vector.</P><PRE><FONT color=#999933>      vector&lt;Date&gt;::iterator first =
            find( e.begin(), e.end(), &quot;01/01/95&quot; );
      vector&lt;Date&gt;::iterator last =
            find( e.begin(), e.end(), &quot;12/31/95&quot; );

      *last = &quot;12/30/95&quot;;</FONT></PRE>  
<P>错误:这儿是非法的,原为last指针可能指向e.end,因此这不是一个有新效的间接引用的iterator</P>  
<P>find算法需要两个参数,如果值没有找到,在这情况里,如果"12/31/95"不在e里面,那么last是等于e.end(),它指向越过容器的底,且是无效的iterator.</P><PRE><FONT color=#999933>      copy( first, last,
            ostream_iterator&lt;Date&gt;( cout, &quot;\n&quot; ) );</FONT></PRE>  
<P>错误:这儿是非法的,原因first指针实际上在last之后。</P>  
<P>如果"01/01/95"在&nbsp;e里面不找到,但"12/31/95"找到了,那么迭代品last将指向在对象集合里的对象(Date对象等于"12/31/95"),但first指向end,然而,copy需要first指向第一个在对象集合的对象,同样last是相似的,[first, last]必须是有效的范围。</P>  
<P>除非你使用标准库的调试版本,或许问题在调试阶段中找到。</P><PRE><FONT color=#999933>      e.insert( --e.end(), TodaysDate() );</FONT></PRE>  
<P>错误:表达式"--e.end()"是不合法的。</P>  
<P>这个理由简单,vector&lt;Date&gt;::iterator是简单的Date*,且你不允许去修改内建类型的临时对象,例如,以下是很明显示是非常代码:</P><PRE>      Date* f();    // 函数返回Date*

      p = --f();    // error, but could be &quot;f() - 1&quot;</PRE>  
<P>幸亏的是,这儿没有效率损失:</P><PRE>      e.insert( e.end() - 1, TodaysDate() );</PRE>  
<P>错误:这儿仍然有其它错误...,如果e是空的,e.end()-1是无效的iterator.</P><PRE><FONT color=#999933>      copy( first, last,
            ostream_iterator&lt;Date&gt;( cout, &quot;\n&quot; ) );
    }</FONT></PRE>  
<P>错误:first与last不是有效的iterator</P>  
<P>当你在插入对象到vector的任何时候,它不是重新分配缓冲,而是在内存块中进行追加,然而,有时vector可能满了,此时增加对象将触发重新分配,这儿,例如当插入操作返回结果,vector可能需要或不需要重新分配,如果它没有这样做,在我们已存在的iterator与copy中可能是无效的。</P>  
<H4>总结:</H4>  
<P>当你使用iterator时,这儿有四点注意:</P>  
<P>1.有效的值:间接引用的iterator是有效吗?,例如,"*e.end()"总是逻辑错误的。</P>  
<P>2.有效的生存期:当你开始使用iterator时候它还仍然有效吗?,当我们获得了经过若干操作后它还有效吗?</P>  
<P>3.有效的范围:成对的iterator是否是有效的范围呢?,first是在last之前吗?两者是否都指向相同的容器里面吗?</P>  
<P>4.非法操作内建类型:例如:在"--e.end()"上面修改内建类型的临时对象(幸运的是,在你的代码中编译器将捕捉到这种错误与类的iterator类型,库的作者允许这种经常在排序任何对象的语法的转换).<br>  
</P>  
</DIV></div></div>  
  
</center></BODY></HTML>  

⌨️ 快捷键说明

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