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

📄 all.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>  <meta name="generator" content=  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">  <title>C++ Strings</title>  <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table>  <tr>  <td>  <div class="body-content">  <div class="header-box">    <a href="../index.html">cppreference.com</a> &gt; <a href=    "index.html">C++ Strings</a>  </div>  <div class="name-format">    append  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  string&amp; append( const string&amp; str );  string&amp; append( const char* str );  string&amp; append( const string&amp; str, <strong>size_type</strong> index, <strong>size_type</strong> len );  string&amp; append( const char* str, <strong>size_type</strong> num );  string&amp; append( <strong>size_type</strong> num, char ch );  string&amp; append( <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end );</pre>  <p>The append() function either:</p>  <ul>    <li>appends <em>str</em> on to the end of the current string,</li>    <li>appends a substring of <em>str</em> starting at <em>index</em>    that is <em>len</em> characters long on to the end of the current    string,</li>    <li>appends <em>num</em> characters of <em>str</em> on to the end    of the current string,</li>    <li>appends <em>num</em> repititions of <em>ch</em> on to the end    of the current string,</li>    <li>or appends the sequence denoted by <em>start</em> and    <em>end</em> on to the end of the current string.</li>  </ul>  <p>For example, the following code uses append() to add 10 copies of  the &#39;!&#39; character to a string:</p>  <pre class="example-code">   string str = &quot;Hello World&quot;;   str.append( 10, &#39;!&#39; );   cout &lt;&lt; str &lt;&lt; endl;             </pre>  <p>That code displays:</p>  <pre class="example-code">   Hello World!!!!!!!!!!                </pre>  <p>In the next example, append() is used to concatenate a substring  of one string onto another string:</p>  <pre class="example-code"> string str1 = &quot;Eventually I stopped caring...&quot;; string str2 = &quot;but that was the &#39;80s so nobody noticed.&quot;; str1.append( str2, 25, 15 ); cout &lt;&lt; &quot;str1 is &quot; &lt;&lt; str1 &lt;&lt; endl; </pre>  <p>When run, the above code displays:</p>  <pre class="example-code"> str1 is Eventually I stopped caring...nobody noticed.          </pre>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    assign  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  void assign( <strong>size_type</strong> num, const char&amp; val );  void assign( <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end );  string&amp; assign( const string&amp; str );  string&amp; assign( const char* str );  string&amp; assign( const char* str, <strong>size_type</strong> num );  string&amp; assign( const string&amp; str, <strong>size_type</strong> index, <strong>size_type</strong> len );  string&amp; assign( <strong>size_type</strong> num, const char&amp; ch );</pre>  <p>The deafult assign() function gives the current string the values  from <em>start</em> to <em>end</em>, or gives it <em>num</em> copies  of <em>val</em>.</p>  <p>In addition to the normal (C++ Lists) <a href=  "../cpplist/assign.html">assign</a>() functionality that all C++  containers have, strings possess an assign() function that also  allows them to:</p>  <ul>    <li>assign <em>str</em> to the current string,</li>    <li>assign the first <em>num</em> characters of <em>str</em> to the    current string,</li>    <li>assign a substring of <em>str</em> starting at <em>index</em>    that is <em>len</em> characters long to the current string,</li>  </ul>  <p>For example, the following code:</p>  <pre class="example-code">   string str1, str2 = &quot;War and Peace&quot;;   str1.assign( str2, 4, 3 );   cout &lt;&lt; str1 &lt;&lt; endl;            </pre>  <p>displays</p>  <pre class="example-code">   and          </pre>  <p>This function will destroy the previous contents of the  string.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    (C++ Lists) <a href="../cpplist/assign.html">assign</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    at  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  <a href="../containers.html">TYPE</a>&amp; at( <strong>size_type</strong> loc );  const <a href="../containers.html">TYPE</a>&amp; at( <strong>size_type</strong> loc ) const;</pre>  <p>The at() function returns a reference to the element in the string  at index <em>loc</em>. The at() function is safer than the []  operator, because it won&#39;t let you reference items outside the  bounds of the string.</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; &quot;Element &quot; &lt;&lt; i &lt;&lt; &quot; is &quot; &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; &quot;Element &quot; &lt;&lt; i &lt;&lt; &quot; is &quot; &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">    Related topics:  </div>  <div class="related-content">    (C++ Multimaps) <a href=    "../cppmultimap/multimap_operators.html">Multimap    operators</a><br>    (C++ Double-ended Queues) <a href=    "../cppdeque/container_operators.html">Container operators</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    begin  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  iterator begin();  const_iterator begin() const;</pre>  <p>The function begin() returns an iterator to the first element of  the string. begin() should run in <a href=  "../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">    Related topics:  </div>  <div class="related-content">    <a href="end.html">end</a><br>    <a href="rbegin.html">rbegin</a><br>    <a href="rend.html">rend</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    c_str  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  const char* c_str();</pre>  <p>The function c_str() returns a const pointer to a regular C  string, identical to the current string. The returned string is  null-terminated.</p>  <p>Note that since the returned pointer is of type <a  href="../keywords/const.html">const</a>, the character data that  c_str() returns <strong>cannot be modified</strong>.  Furthermore,  you do not need to call <a  href="../stdmem/free.html">free()</a> or <a  href="../keywords/delete.html">delete</a>  on this pointer.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="string_operators.html">String operators</a><br>    <a href="data.html">data</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    capacity  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  <strong>size_type</strong> capacity() const;</pre>  <p>The capacity() function returns the number of elements that the  string can hold before it will need to allocate more space.</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; &quot;The capacity of v1 is &quot; &lt;&lt; v1.capacity() &lt;&lt; endl; vector&lt;int&gt; v2; v2.reserve(20); cout &lt;&lt; &quot;The capacity of v2 is &quot; &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=  "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=  "../complexity.html">constant time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="reserve.html">reserve</a><br>    <a href="resize.html">resize</a><br>    <a href="size.html">size</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    clear  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  void clear();</pre>  <p>The function clear() deletes all of the elements in the string.  clear() runs in <a href="../complexity.html">linear time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    (C++ Lists) <a href="../cpplist/erase.html">erase</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    compare  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  int compare( const string&amp; str );  int compare( const char* str );  int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const string&amp; str );  int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const string&amp; str, <strong>size_type</strong> index2,  <strong>size_type</strong> length2 );  int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const char* str, <strong>size_type</strong> length2 );</pre>  <p>The compare() function either compares <em>str</em> to the current  string in a variety of ways, returning</p>  <table class="code-table">    <tr>      <th class="code-table-th">Return Value</th>      <th class="code-table-th">Case</th>    </tr>    <tr>      <td class="code-table-td">less than zero</td>      <td class="code-table-td">this &lt; str</td>    </tr>    <tr>      <td class="code-table-td">zero</td>      <td class="code-table-td">this == str</td>    </tr>    <tr>      <td class="code-table-td">greater than zero</td>      <td class="code-table-td">this &gt; str</td>    </tr>  </table>  <p>The various functions either:</p>  <ul>    <li>compare <em>str</em> to the current string,</li>    <li>compare <em>str</em> to a substring of the current string,    starting at <em>index</em> for <em>length</em> characters,</li>    <li>compare a substring of <em>str</em> to a substring of the    current string, where <em>index2</em> and <em>length2</em> refer to    <em>str</em> and <em>index</em> and <em>length</em> refer to the    current string,</li>    <li>or compare a substring of <em>str</em> to a substring of the    current string, where the substring of <em>str</em> begins at zero    and is <em>length2</em> characters long, and the substring of the    current string begins at <em>index</em> and is <em>length</em>    characters long.</li>  </ul>  <p>For example, the following code uses compare() to compare four  strings with eachother:</p>  <pre class="example-code"> string names[] = {&quot;Homer&quot;, &quot;Marge&quot;, &quot;3-eyed fish&quot;, &quot;inanimate carbon rod&quot;};             for( int i = 0; i &lt; 4; i++ ) {   for( int j = 0; j &lt; 4; j++ ) {     cout &lt;&lt; names[i].compare( names[j] ) &lt;&lt; &quot; &quot;;   }   cout &lt;&lt; endl; }              </pre>  <p>Data from the above code was used to generate this table, which  shows how the various strings compare to eachother:</p>  <table class="code-table">    <tr>      <th class="code-table-th"></th>      <th class="code-table-th">Homer</th>      <th class="code-table-th">Marge</th>

⌨️ 快捷键说明

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