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

📄 wwwtc6.htm

📁 C++builder学习资料C++builder
💻 HTM
字号:


<HTML>

<HEAD>

   <TITLE>What's Wrong With This Code? Volume #6: The STL</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY>



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">

<TR>



<TD>







<H2>

What's Wrong With This Code? Volume #6

</H2>



<H4>

The STL<BR>

<I> by Chris Uzdavinis</I>

</H4>



<P>

A programmer has a collection of pointers to objects of the

following class <TT>A</TT>.

</P>

<pre>

<b>class</b> A

<b>{</b>

<b>public</b><b>:</b>

  <b>void</b> do_something<b>(</b><b>)</b><b>;</b>

  <b>bool</b> is_even<b>(</b><b>)</b> <b>const</b><b>;</b>

<b>}</b><b>;</b>

</pre>

<P>

Here is the global typedef and declarations for a couple of vector containers, <TT>v1</TT> and <TT>v2</TT>.

</P>

<pre>

<b>typedef</b> std<b>:</b><b>:</b>vector&lt;A<b>*</b><b>></b> V<b>;</b>

V  v1<b>,</b> v2 <b>;</b>

</pre>

<P>

Given the above declarations, find what can be improved about the

following code snippets. Some have incorrect behavior, others are

just inefficient. Some simply have better ways to be implemented.

Others have POTENTIAL to cause problems but don't always do so.

Assume v1 and v2 are global, for simplicity, and that the pointer

elements are never null. (If you're careful you can make such a

guarentee, and we are assuming it here.)



Finally, assume each code snippit is independent from all the

others. That is, code in item #1 doesn't affect code in item #2,

neither affect #3, and so on.

</P>



<P>

<B>Question #1:</B>

</P>

<pre>

<font color="navy">// assign contents of v2 to v1</font>

v1 <b>=</b> v2<b>;</b>

</pre>

<P>

What can potentially go wrong here? What considerations should

you make when doing this?

</P>



<P>

<B>Question #2:</B>

</P>

<pre>

<font color="navy">// delete the element in position 5</font>

<b>void</b> f<b>(</b><b>)</b>

<b>{</b>

  <b>if</b> <b>(</b>v1<b>.</b>size<b>(</b><b>)</b> <b>></b> <font color="blue">5</font><b>)</b>

  <b>{</b>

    <b>delete</b> v1<b>[</b><font color="blue">5</font><b>]</b><b>;</b>

  <b>}</b>

<b>}</b>

</pre>

<P>

Why is calling <TT>f()</TT> most likely going to lead to a program crash?

</P>





<P>

<B>Question #3:</B>

</P>

<pre>

<font color="navy">// copy v1 into v2</font>

<b>void</b> f<b>(</b><b>)</b>

<b>{</b>

  std<b>:</b><b>:</b>copy<b>(</b>v1<b>.</b>begin<b>(</b><b>)</b><b>,</b> v1<b>.</b>end<b>(</b><b>)</b><b>,</b> v2<b>.</b>begin<b>(</b><b>)</b><b>)</b><b>;</b>

<b>}</b>

</pre>

<P>

How can this blow up?

</P>





<P>

<B>Question #4:</B>

</P>

<pre>

<font color="navy">// find an element and remove it from container.</font>

<font color="navy">// assume the caller keeps a reference to a so it</font>

<font color="navy">// isn't leaked.</font>

<b>void</b> f<b>(</b>A <b>*</b>a<b>)</b>

<b>{</b>

  V<b>:</b><b>:</b>iterator i <b>=</b> std<b>:</b><b>:</b>find<b>(</b>v1<b>.</b>begin<b>(</b><b>)</b><b>,</b> v1<b>.</b>end<b>(</b><b>)</b><b>,</b> a<b>)</b><b>;</b>

  <b>if</b> <b>(</b>i<b>)</b>

  <b>{</b>

    v1<b>.</b>erase<b>(</b>i<b>)</b><b>;</b>

  <b>}</b>

<b>}</b>

</pre>

<P>

What is the subtle bug here that has serious consequences?

</P>





<P>

<B>Question #5:</B>

</P>

<pre>

<font color="navy">// count all a's in the range that are "special"</font>

<b>extern</b> <b>bool</b> is_special_value<b>(</b>A <b>*</b><b>)</b><b>;</b>



<b>int</b> count_special_values<b>(</b>V <b>const</b> <b>&</b> v<b>)</b>

<b>{</b>

  <b>int</b> count <b>=</b> <font color="blue">0</font><b>;</b>

  V<b>:</b><b>:</b>iterator i <b>=</b> v<b>.</b>begin<b>(</b><b>)</b><b>;</b>

  <b>while</b> <b>(</b>i <b>!=</b> v<b>.</b>end<b>(</b><b>)</b><b>)</b>

  <b>{</b>

    <b>if</b> <b>(</b>is_special_value<b>(</b><b>*</b>i<b>)</b><b>)</b>

    <b>{</b>

      <b>++</b>count<b>;</b>

    <b>}</b>

    i<b>++</b><b>;</b>

  <b>}</b>

  <b>return</b> count<b>;</b>

<b>}</b>

</pre>

<P>

Why doesn't this compile?  Once it compiles, what can be improved

about this function?

</P>





<P>

<B>Question #6:</B>

</P>

<pre>

<font color="navy">// erase all members of v1</font>

<b>void</b> f<b>(</b><b>)</b>

<b>{</b>

  V<b>:</b><b>:</b>iterator i<b>;</b>

  V<b>:</b><b>:</b>iterator end <b>=</b> v1<b>.</b>end<b>(</b><b>)</b><b>;</b>

  <b>for</b> <b>(</b>i<b>=</b> v1<b>.</b>begin<b>(</b><b>)</b><b>;</b> i <b>!=</b> end<b>;</b> <b>++</b>i<b>)</b>

  <b>{</b>

    v1<b>.</b>erase<b>(</b>i<b>)</b><b>;</b>

  <b>}</b>

<b>}</b>

</pre>

<P>

What's wrong with the assumptions in this code?  How can this be made

more efficient?

</P>





<P>

<B>Question #7:</B>

</P>

<pre>

<font color="navy">// remove even-valued entries from the list</font>

<b>typedef</b> std<b>:</b><b>:</b>list&lt;A<b>*</b><b>></b> List<b>;</b>



<b>void</b> f<b>(</b>List <b>&</b> lst<b>)</b>

<b>{</b>

  <font color="navy">// now operate on the list</font>

  List<b>:</b><b>:</b>iterator i <b>=</b> lst<b>.</b>begin<b>(</b><b>)</b><b>;</b>

  <b>while</b> <b>(</b>i <b>!=</b> lst<b>.</b>end<b>(</b><b>)</b><b>)</b>

  <b>{</b>

   <b>if</b> <b>(</b><b>(</b><b>*</b>i<b>)</b><b>-></b>is_even<b>(</b><b>)</b><b>)</b>

   <b>{</b>

     lst<b>.</b>erase<b>(</b>i<b>)</b><b>;</b>

   <b>}</b>

   <b>++</b>i<b>;</b>

  <b>}</b>

<b>}</b>

</pre>

<P>

What common mistake is the programmer making?

</P>





<P>

<B>Question #8:</B>

</P>

<pre>

std<b>:</b><b>:</b>mismatch<b>(</b>v1<b>.</b>begin<b>(</b><b>)</b><b>,</b> v2<b>.</b>begin<b>(</b><b>)</b><b>,</b> v2<b>.</b>end<b>(</b><b>)</b><b>)</b><b>;</b>

</pre>

<P>

Something is wrong with this call that has undefined runtime behavior

(though you can reasonably expect a crash.)  What?

</P>





<P>

<B>Question #9:</B>

</P>

<pre>

<font color="navy">// call do_something_with() for each item in v1 that is not even</font>

<b>extern</b> <b>void</b> do_something_with<b>(</b>A<b>*</b><b>)</b><b>;</b>



<b>void</b> f<b>(</b><b>)</b>

<b>{</b>

  V<b>:</b><b>:</b>iterator i <b>=</b> v1<b>.</b>begin<b>(</b><b>)</b><b>;</b>

  <b>while</b> <b>(</b>i <b>!=</b> v1<b>.</b>end<b>(</b><b>)</b><b>)</b>

  <b>{</b>

    <b>if</b> <b>(</b><b>(</b><b>*</b>i<b>)</b><b>-></b>is_even<b>(</b><b>)</b><b>)</b>

    <b>{</b>

      <font color="navy">// skip this item</font>

      <b>++</b>i<b>;</b>

    <b>}</b>

    do_something_with<b>(</b><b>*</b>i<b>)</b><b>;</b>

    i<b>++</b><b>;</b>

  <b>}</b>

<b>}</b>

</pre>

<P>

What bugs are in this code?  It not only contains logic bugs (it

doesn't do what the documentation (comment at top) says), but it

has iterator problems as well.  How many can you find?

</P>







<H4>

<A TARGET=_top href="wwwtc6answer.htm">Answers</A>

</H4>

<BR>







</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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