OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [libstdc++-v3/] [doc/] [html/] [manual/] [bk01pt05ch13s06.html] - Blame information for rev 816

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 424 jeremybenn
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CString (MFC)</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><meta name="keywords" content="&#10;      ISO C++&#10;    , &#10;      library&#10;    " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="bk01pt05ch13.html" title="Chapter 13. String Classes" /><link rel="prev" href="bk01pt05ch13s05.html" title="Shrink to Fit" /><link rel="next" href="localization.html" title="Part VI.  Localization" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">CString (MFC)</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="bk01pt05ch13s05.html">Prev</a> </td><th width="60%" align="center">Chapter 13. String Classes</th><td width="20%" align="right"> <a accesskey="n" href="localization.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="CString (MFC)"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="strings.string.Cstring"></a>CString (MFC)</h2></div></div></div><p>
4
    </p><p>A common lament seen in various newsgroups deals with the Standard
5
      string class as opposed to the Microsoft Foundation Class called
6
      CString.  Often programmers realize that a standard portable
7
      answer is better than a proprietary nonportable one, but in porting
8
      their application from a Win32 platform, they discover that they
9
      are relying on special functions offered by the CString class.
10
   </p><p>Things are not as bad as they seem.  In
11
      <a class="ulink" href="http://gcc.gnu.org/ml/gcc/1999-04n/msg00236.html" target="_top">this
12
      message</a>, Joe Buck points out a few very important things:
13
   </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>The Standard <code class="code">string</code> supports all the operations
14
             that CString does, with three exceptions.
15
         </p></li><li class="listitem"><p>Two of those exceptions (whitespace trimming and case
16
             conversion) are trivial to implement.  In fact, we do so
17
             on this page.
18
         </p></li><li class="listitem"><p>The third is <code class="code">CString::Format</code>, which allows formatting
19
             in the style of <code class="code">sprintf</code>.  This deserves some mention:
20
         </p></li></ul></div><p>
21
      The old libg++ library had a function called form(), which did much
22
      the same thing.  But for a Standard solution, you should use the
23
      stringstream classes.  These are the bridge between the iostream
24
      hierarchy and the string class, and they operate with regular
25
      streams seamlessly because they inherit from the iostream
26
      hierarchy.  An quick example:
27
   </p><pre class="programlisting">
28
   #include &lt;iostream&gt;
29
   #include &lt;string&gt;
30
   #include &lt;sstream&gt;
31
 
32
   string f (string&amp; incoming)     // incoming is "foo  N"
33
   {
34
       istringstream   incoming_stream(incoming);
35
       string          the_word;
36
       int             the_number;
37
 
38
       incoming_stream &gt;&gt; the_word        // extract "foo"
39
                       &gt;&gt; the_number;     // extract N
40
 
41
       ostringstream   output_stream;
42
       output_stream &lt;&lt; "The word was " &lt;&lt; the_word
43
                     &lt;&lt; " and 3*N was " &lt;&lt; (3*the_number);
44
 
45
       return output_stream.str();
46
   } </pre><p>A serious problem with CString is a design bug in its memory
47
      allocation.  Specifically, quoting from that same message:
48
   </p><pre class="programlisting">
49
   CString suffers from a common programming error that results in
50
   poor performance.  Consider the following code:
51
 
52
   CString n_copies_of (const CString&amp; foo, unsigned n)
53
   {
54
           CString tmp;
55
           for (unsigned i = 0; i &lt; n; i++)
56
                   tmp += foo;
57
           return tmp;
58
   }
59
 
60
   This function is O(n^2), not O(n).  The reason is that each +=
61
   causes a reallocation and copy of the existing string.  Microsoft
62
   applications are full of this kind of thing (quadratic performance
63
   on tasks that can be done in linear time) -- on the other hand,
64
   we should be thankful, as it's created such a big market for high-end
65
   ix86 hardware. :-)
66
 
67
   If you replace CString with string in the above function, the
68
   performance is O(n).
69
   </pre><p>Joe Buck also pointed out some other things to keep in mind when
70
      comparing CString and the Standard string class:
71
   </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>CString permits access to its internal representation; coders
72
             who exploited that may have problems moving to <code class="code">string</code>.
73
         </p></li><li class="listitem"><p>Microsoft ships the source to CString (in the files
74
             MFC\SRC\Str{core,ex}.cpp), so you could fix the allocation
75
             bug and rebuild your MFC libraries.
76
             <span class="emphasis"><em><span class="emphasis"><em>Note:</em></span> It looks like the CString shipped
77
             with VC++6.0 has fixed this, although it may in fact have been
78
             one of the VC++ SPs that did it.</em></span>
79
         </p></li><li class="listitem"><p><code class="code">string</code> operations like this have O(n) complexity
80
             <span class="emphasis"><em>if the implementors do it correctly</em></span>.  The libstdc++
81
             implementors did it correctly.  Other vendors might not.
82
         </p></li><li class="listitem"><p>While parts of the SGI STL are used in libstdc++, their
83
             string class is not.  The SGI <code class="code">string</code> is essentially
84
             <code class="code">vector&lt;char&gt;</code> and does not do any reference
85
             counting like libstdc++'s does.  (It is O(n), though.)
86
             So if you're thinking about SGI's string or rope classes,
87
             you're now looking at four possibilities:  CString, the
88
             libstdc++ string, the SGI string, and the SGI rope, and this
89
             is all before any allocator or traits customizations!  (More
90
             choices than you can shake a stick at -- want fries with that?)
91
         </p></li></ul></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="bk01pt05ch13s05.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="bk01pt05ch13.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="localization.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Shrink to Fit </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Part VI. 
92
  Localization
93
 
94
</td></tr></table></div></body></html>

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.