﻿<?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++博客-MemoryGarden's Blog-随笔分类-STL</title><link>http://www.cppblog.com/MemoryGarden/category/12634.html</link><description>努力

                                  -----------大能猫</description><language>zh-cn</language><lastBuildDate>Fri, 01 Jan 2010 10:08:39 GMT</lastBuildDate><pubDate>Fri, 01 Jan 2010 10:08:39 GMT</pubDate><ttl>60</ttl><item><title>string::compare</title><link>http://www.cppblog.com/MemoryGarden/archive/2009/12/30/104428.html</link><dc:creator>memorygarden</dc:creator><author>memorygarden</author><pubDate>Tue, 29 Dec 2009 18:11:00 GMT</pubDate><guid>http://www.cppblog.com/MemoryGarden/archive/2009/12/30/104428.html</guid><wfw:comment>http://www.cppblog.com/MemoryGarden/comments/104428.html</wfw:comment><comments>http://www.cppblog.com/MemoryGarden/archive/2009/12/30/104428.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MemoryGarden/comments/commentRss/104428.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MemoryGarden/services/trackbacks/104428.html</trackback:ping><description><![CDATA[<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;compare&nbsp;(&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;str&nbsp;)&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;compare&nbsp;(&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;s&nbsp;)&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br></span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;compare&nbsp;(&nbsp;size_t&nbsp;pos1,&nbsp;size_t&nbsp;n1,&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;str&nbsp;)&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br></span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;compare&nbsp;(&nbsp;size_t&nbsp;pos1,&nbsp;size_t&nbsp;n1,&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;s)&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br></span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;compare&nbsp;(&nbsp;size_t&nbsp;pos1,&nbsp;size_t&nbsp;n1,&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;str,&nbsp;size_t&nbsp;pos2,&nbsp;size_t&nbsp;n2&nbsp;)&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br></span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;compare&nbsp;(&nbsp;size_t&nbsp;pos1,&nbsp;size_t&nbsp;n1,&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;s,&nbsp;size_t&nbsp;n2)&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;</span></div>
<br><br><br>Compares the content of this object (or a substring of it, known as <em>compared (sub)string</em>) to the content of a <em>comparing string</em>, which is formed according to the arguments passed.<br>
The member function returns <tt>0</tt> if all the characters in the
compared contents compare equal, a negative value if the first
character that does not match compares to less in the object than in
the <em>comparing string</em>, and a positive value in the opposite case.<br>
Notice that for <a  href="http://www.cplusplus.com/string">string</a> objects, the result of a character comparison depends only on its character code (i.e., its <a  href="http://www.cplusplus.com/ASCII">ASCII</a> code), so the result has some limited alphabetical or numerical ordering meaning.<br>
For other <tt>basic_string</tt> class instantitations, the comparison depends on the specific <a  href="http://www.cplusplus.com/char_traits">traits</a>::compare function, where <tt>traits</tt> is one of the class template parameters.<br>
<tt><br><br>0</tt> if the compared characters sequences are equal, otherwise a number different from <tt>0</tt> is returned, with its sign indicating whether the object is considered greater than the <em>comparing string</em> passed as parameter (positive sign), or smaller (negative sign).<br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;comparing&nbsp;apples&nbsp;with&nbsp;apples</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">#include&nbsp;</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;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#include&nbsp;</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;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;std;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str1&nbsp;(</span><span style="color: #000000;">"</span><span style="color: #000000;">green&nbsp;apple</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str2&nbsp;(</span><span style="color: #000000;">"</span><span style="color: #000000;">red&nbsp;apple</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(str1.compare(str2)&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str1&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;is&nbsp;not&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str2&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(str1.compare(</span><span style="color: #000000;">6</span><span style="color: #000000;">,</span><span style="color: #000000;">5</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">apple</span><span style="color: #000000;">"</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">still,&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str1&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;is&nbsp;an&nbsp;apple\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(str2.compare(str2.size()</span><span style="color: #000000;">-</span><span style="color: #000000;">5</span><span style="color: #000000;">,</span><span style="color: #000000;">5</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">apple</span><span style="color: #000000;">"</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">and&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str2&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;is&nbsp;also&nbsp;an&nbsp;apple\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(str1.compare(</span><span style="color: #000000;">6</span><span style="color: #000000;">,</span><span style="color: #000000;">5</span><span style="color: #000000;">,str2,</span><span style="color: #000000;">4</span><span style="color: #000000;">,</span><span style="color: #000000;">5</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">therefore,&nbsp;both&nbsp;are&nbsp;apples\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br><br><br><br><br><img src ="http://www.cppblog.com/MemoryGarden/aggbug/104428.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MemoryGarden/" target="_blank">memorygarden</a> 2009-12-30 02:11 <a href="http://www.cppblog.com/MemoryGarden/archive/2009/12/30/104428.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>string::clear</title><link>http://www.cppblog.com/MemoryGarden/archive/2009/12/30/104426.html</link><dc:creator>memorygarden</dc:creator><author>memorygarden</author><pubDate>Tue, 29 Dec 2009 17:54:00 GMT</pubDate><guid>http://www.cppblog.com/MemoryGarden/archive/2009/12/30/104426.html</guid><wfw:comment>http://www.cppblog.com/MemoryGarden/comments/104426.html</wfw:comment><comments>http://www.cppblog.com/MemoryGarden/archive/2009/12/30/104426.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MemoryGarden/comments/commentRss/104426.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MemoryGarden/services/trackbacks/104426.html</trackback:ping><description><![CDATA[The string content is set to an empty string, erasing any previous content and thus leaving its <a  href="http://www.cplusplus.com/size">size</a> at 0 characters.<br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;string::clear</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">#include&nbsp;</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;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#include&nbsp;</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;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;std;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str;<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;c;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Please&nbsp;type&nbsp;some&nbsp;lines&nbsp;of&nbsp;text.&nbsp;Enter&nbsp;a&nbsp;period&nbsp;to&nbsp;finish:\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">do</span><span style="color: #000000;">&nbsp;{<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="color: #000000;">=</span><span style="color: #000000;">cin.</span><span style="color: #0000ff;">get</span><span style="color: #000000;">();<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;str&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;c;<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(c</span><span style="color: #000000;">==</span><span style="color: #000000;">'</span><span style="color: #000000;">\n</span><span style="color: #000000;">'</span><span style="color: #000000;">)<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str;<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str.clear();<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;(c</span><span style="color: #000000;">!=</span><span style="color: #000000;">'</span><span style="color: #000000;">.</span><span style="color: #000000;">'</span><span style="color: #000000;">);<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><img src ="http://www.cppblog.com/MemoryGarden/aggbug/104426.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MemoryGarden/" target="_blank">memorygarden</a> 2009-12-30 01:54 <a href="http://www.cppblog.com/MemoryGarden/archive/2009/12/30/104426.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>string::capacity</title><link>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104247.html</link><dc:creator>memorygarden</dc:creator><author>memorygarden</author><pubDate>Sun, 27 Dec 2009 18:21:00 GMT</pubDate><guid>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104247.html</guid><wfw:comment>http://www.cppblog.com/MemoryGarden/comments/104247.html</wfw:comment><comments>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104247.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MemoryGarden/comments/commentRss/104247.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MemoryGarden/services/trackbacks/104247.html</trackback:ping><description><![CDATA[string::capacity<br><br>Returns the size of the allocated storage space in the <a  href="http://www.cplusplus.com/string">string</a> object.<br>
<br>
Notice that the capacity is not necessarily equal to the number of
characters that conform the content of the string (this can be obtained
with members <a  href="http://www.cplusplus.com/string::size">size</a> or <a  href="http://www.cplusplus.com/string::length">length</a>), but the capacity of the allocated space, which is either equal or greater than this content size.<br>
<br>
Notice also that this capacity does not suppose a limit to the length
of the string. If more space is required to accomodate content in the <a  href="http://www.cplusplus.com/string">string</a> object, the capacity is automatically expanded, or can even be explicitly modified by calling member <a  href="http://www.cplusplus.com/string::reserve">reserve</a>.<br>
<br>
The real limit on the size a <a  href="http://www.cplusplus.com/string">string</a> object can reach is returned by member <a  href="http://www.cplusplus.com/string::max_size">max_size</a>.<br>
<br><br><br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">size_t&nbsp;capacity&nbsp;(&nbsp;)&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;</span></div>
<br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;comparing&nbsp;size,&nbsp;length,&nbsp;capacity&nbsp;and&nbsp;max_size</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">#include&nbsp;</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;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#include&nbsp;</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;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;std;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str&nbsp;(</span><span style="color: #000000;">"</span><span style="color: #000000;">Test&nbsp;string</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">size:&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str.size()&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">length:&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str.length()&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">capacity:&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str.capacity()&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">max_size:&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str.max_size()&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br><br><br><br><img src ="http://www.cppblog.com/MemoryGarden/aggbug/104247.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MemoryGarden/" target="_blank">memorygarden</a> 2009-12-28 02:21 <a href="http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104247.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>string::begin</title><link>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104246.html</link><dc:creator>memorygarden</dc:creator><author>memorygarden</author><pubDate>Sun, 27 Dec 2009 18:00:00 GMT</pubDate><guid>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104246.html</guid><wfw:comment>http://www.cppblog.com/MemoryGarden/comments/104246.html</wfw:comment><comments>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104246.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MemoryGarden/comments/commentRss/104246.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MemoryGarden/services/trackbacks/104246.html</trackback:ping><description><![CDATA[string::begin<br><br>Returns an iterator referring to the first character in the string.<br><br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iterator&nbsp;begin();<br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">const_iterator&nbsp;begin()&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;</span></div>
<br><br><br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;string::begin&nbsp;and&nbsp;string::end</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">#include&nbsp;</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;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#include&nbsp;</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;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;std;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str&nbsp;(</span><span style="color: #000000;">"</span><span style="color: #000000;">Test&nbsp;string</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">::iterator&nbsp;it;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(&nbsp;it</span><span style="color: #000000;">=</span><span style="color: #000000;">str.begin()&nbsp;;&nbsp;it&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;str.end();&nbsp;it</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;)<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">it;<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br><img src ="http://www.cppblog.com/MemoryGarden/aggbug/104246.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MemoryGarden/" target="_blank">memorygarden</a> 2009-12-28 02:00 <a href="http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104246.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>string::at</title><link>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104245.html</link><dc:creator>memorygarden</dc:creator><author>memorygarden</author><pubDate>Sun, 27 Dec 2009 17:52:00 GMT</pubDate><guid>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104245.html</guid><wfw:comment>http://www.cppblog.com/MemoryGarden/comments/104245.html</wfw:comment><comments>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104245.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MemoryGarden/comments/commentRss/104245.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MemoryGarden/services/trackbacks/104245.html</trackback:ping><description><![CDATA[string::at<br><br>This member function behaves as <tt>operator[] </tt>, except that <a href="http://www.cplusplus.com/string::at">at</a> also performs a range check, throwing an exception of type <tt>out_of_range</tt> in case that <em>pos</em> is not an actual position in the string<br><br>和[]运算符不同的地方是当它接受一个非法的参数的时候,会抛出异常<br><br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;at&nbsp;(&nbsp;size_t&nbsp;pos&nbsp;)&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;at&nbsp;(&nbsp;size_t&nbsp;pos&nbsp;);</span></div>
<br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;string::at</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">#include&nbsp;</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;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#include&nbsp;</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;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;std;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str&nbsp;(</span><span style="color: #000000;">"</span><span style="color: #000000;">Test&nbsp;string</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;str.length();&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;{<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str.at(i);<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;}<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">}</span></div>
<br> <img src ="http://www.cppblog.com/MemoryGarden/aggbug/104245.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MemoryGarden/" target="_blank">memorygarden</a> 2009-12-28 01:52 <a href="http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104245.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>string::assign</title><link>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104244.html</link><dc:creator>memorygarden</dc:creator><author>memorygarden</author><pubDate>Sun, 27 Dec 2009 17:48:00 GMT</pubDate><guid>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104244.html</guid><wfw:comment>http://www.cppblog.com/MemoryGarden/comments/104244.html</wfw:comment><comments>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104244.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MemoryGarden/comments/commentRss/104244.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MemoryGarden/services/trackbacks/104244.html</trackback:ping><description><![CDATA[string::assign&nbsp; <br>Assigns new content to the string replacing its current content.<br><br>return&nbsp; *this<br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;string::assign</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">#include&nbsp;</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;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#include&nbsp;</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;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;std;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str;<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">base</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">The&nbsp;quick&nbsp;brown&nbsp;fox&nbsp;jumps&nbsp;over&nbsp;a&nbsp;lazy&nbsp;dog.</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;used&nbsp;in&nbsp;the&nbsp;same&nbsp;order&nbsp;as&nbsp;described&nbsp;above:</span><span style="color: #008000;"><br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;str.assign(</span><span style="color: #0000ff;">base</span><span style="color: #000000;">);<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;str.assign(</span><span style="color: #0000ff;">base</span><span style="color: #000000;">,</span><span style="color: #000000;">10</span><span style="color: #000000;">,</span><span style="color: #000000;">9</span><span style="color: #000000;">);<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"brown&nbsp;fox"</span><span style="color: #008000;"><br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;str.assign(</span><span style="color: #000000;">"</span><span style="color: #000000;">pangrams&nbsp;are&nbsp;cool</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">7</span><span style="color: #000000;">);<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"pangram"</span><span style="color: #008000;"><br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;str.assign(</span><span style="color: #000000;">"</span><span style="color: #000000;">c-string</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"c-string"</span><span style="color: #008000;"><br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;str.assign(</span><span style="color: #000000;">10</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">*</span><span style="color: #000000;">'</span><span style="color: #000000;">);<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"**********"</span><span style="color: #008000;"><br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;str.assign</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #000000;">10</span><span style="color: #000000;">,</span><span style="color: #000000;">0x2D</span><span style="color: #000000;">);<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"----------"</span><span style="color: #008000;"><br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;str.assign(</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.begin()</span><span style="color: #000000;">+</span><span style="color: #000000;">16</span><span style="color: #000000;">,</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.end()</span><span style="color: #000000;">-</span><span style="color: #000000;">12</span><span style="color: #000000;">);<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"fox&nbsp;jumps&nbsp;over"</span><span style="color: #008000;"><br></span><span style="color: #008080;">33</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">35</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br> <img src ="http://www.cppblog.com/MemoryGarden/aggbug/104244.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MemoryGarden/" target="_blank">memorygarden</a> 2009-12-28 01:48 <a href="http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104244.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>string::append</title><link>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104243.html</link><dc:creator>memorygarden</dc:creator><author>memorygarden</author><pubDate>Sun, 27 Dec 2009 17:41:00 GMT</pubDate><guid>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104243.html</guid><wfw:comment>http://www.cppblog.com/MemoryGarden/comments/104243.html</wfw:comment><comments>http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104243.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MemoryGarden/comments/commentRss/104243.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MemoryGarden/services/trackbacks/104243.html</trackback:ping><description><![CDATA[string::append&nbsp; 在调用的string后面追加<br><br>return&nbsp;&nbsp; *this<br><br><br>c plusplus 代码:<br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;appending&nbsp;to&nbsp;string</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">#include&nbsp;</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;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#include&nbsp;</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;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;std;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str;<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str2</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">Writing&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;str3</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">print&nbsp;10&nbsp;and&nbsp;then&nbsp;5&nbsp;more</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;used&nbsp;in&nbsp;the&nbsp;same&nbsp;order&nbsp;as&nbsp;described&nbsp;above:</span><span style="color: #008000;"><br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;str.append(str2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"Writing&nbsp;"</span><span style="color: #008000;"><br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;str.append(str3,</span><span style="color: #000000;">6</span><span style="color: #000000;">,</span><span style="color: #000000;">3</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"10&nbsp;"</span><span style="color: #008000;"><br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;str.append(</span><span style="color: #000000;">"</span><span style="color: #000000;">dots&nbsp;are&nbsp;cool</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">5</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"dots&nbsp;"</span><span style="color: #008000;"><br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;str.append(</span><span style="color: #000000;">"</span><span style="color: #000000;">here:&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"here:&nbsp;"</span><span style="color: #008000;"><br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;str.append(</span><span style="color: #000000;">10</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">.</span><span style="color: #000000;">'</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"<img src="http://www.cppblog.com/Images/dot.gif"><img src="http://www.cppblog.com/Images/dot.gif"><img src="http://www.cppblog.com/Images/dot.gif">."</span><span style="color: #008000;"><br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;str.append(str3.begin()</span><span style="color: #000000;">+</span><span style="color: #000000;">8</span><span style="color: #000000;">,str3.end());&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"&nbsp;and&nbsp;then&nbsp;5&nbsp;more"</span><span style="color: #008000;"><br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;str.append</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #000000;">5</span><span style="color: #000000;">,</span><span style="color: #000000;">0x2E</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"<img src="http://www.cppblog.com/Images/dot.gif">.."</span><span style="color: #008000;"><br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;str&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br><br><br> <img src ="http://www.cppblog.com/MemoryGarden/aggbug/104243.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MemoryGarden/" target="_blank">memorygarden</a> 2009-12-28 01:41 <a href="http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104243.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>