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

📄 all.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
📖 第 1 页 / 共 4 页
字号:
  string&amp; replace( <strong>size_type</strong> index1, <strong>size_type</strong> num1, const string&amp; str, <strong>size_type</strong> index2, <strong>size_type</strong> num2 );  string&amp; replace( <strong>size_type</strong> index, <strong>size_type</strong> num, const char* str );  string&amp; replace( <strong>size_type</strong> index, <strong>size_type</strong> num1, const char* str, <strong>size_type</strong> num2 );  string&amp; replace( <strong>size_type</strong> index, <strong>size_type</strong> num1, <strong>size_type</strong> num2, char ch );  string&amp; replace( iterator start, iterator end, const string&amp; str );  string&amp; replace( iterator start, iterator end, const char* str );  string&amp; replace( iterator start, iterator end, const char* str, <strong>size_type</strong> num );  string&amp; replace( iterator start, iterator end, <strong>size_type</strong> num, char ch );</pre>  <p>The function replace() either:</p>  <ul>    <li>replaces characters of the current string with up to    <em>num</em> characters from <em>str</em>, beginning at    <em>index</em>,</li>    <li>replaces up to <em>num1</em> characters of the current string    (starting at <em>index1</em>) with up to <em>num2</em> characters    from <em>str</em> beginning at <em>index2</em>,</li>    <li>replaces up to <em>num</em> characters of the current string    with characters from <em>str</em>, beginning at <em>index</em> in    <em>str</em>,</li>    <li>replaces up to <em>num1</em> characters in the current string    (beginning at <em>index1</em>) with <em>num2</em> characters from    <em>str</em> beginning at <em>index2</em>,</li>    <li>replaces up to <em>num1</em> characters in the current string    (beginning at <em>index</em>) with <em>num2</em> copies of    <em>ch</em>,</li>    <li>replaces the characters in the current string from    <em>start</em> to <em>end</em> with <em>str</em>,</li>    <li>replaces characters in the current string from <em>start</em>    to <em>end</em> with <em>num</em> characters from    <em>str</em>,</li>    <li>or replaces the characters in the current string from    <em>start</em> to <em>end</em> with <em>num</em> copies of    <em>ch</em>.</li>  </ul>  <p>For example, the following code displays the string &quot;They say  he carved it himself...find your soul-mate, Homer.&quot;</p>  <pre class="example-code">   string s = &quot;They say he carved it himself...from a BIGGER spoon&quot;;   string s2 = &quot;find your soul-mate, Homer.&quot;;   s.replace( 32, s2.length(), s2 );   cout &lt;&lt; s &lt;&lt; endl;               </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="insert.html">insert</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    reserve  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  void reserve( <strong>size_type</strong> size );</pre>  <p>The reserve() function sets the capacity of the string to at least  <em>size</em>.</p>  <p>reserve() runs in <a href="../complexity.html">linear  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="capacity.html">capacity</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    resize  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  void resize( <strong>size_type</strong> size, const <a href="../containers.html">TYPE</a>&amp; val = <a href="../containers.html">TYPE</a>() );</pre>  <p>The function resize() changes the size of the string to  <em>size</em>. If <em>val</em> is specified then any newly-created  elements will be initialized to have a value of <em>val</em>.</p>  <p>This function runs in <a href="../complexity.html">linear  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    (C++ Multimaps) <a href=    "../cppmultimap/multimap_constructors.html">Multimap constructors    &amp; destructors</a><br>    <a href="capacity.html">capacity</a><br>    <a href="size.html">size</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    rfind  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  <strong>size_type</strong> rfind( const string&amp; str, <strong>size_type</strong> index );  <strong>size_type</strong> rfind( const char* str, <strong>size_type</strong> index );  <strong>size_type</strong> rfind( const char* str, <strong>size_type</strong> index, <strong>size_type</strong> num );  <strong>size_type</strong> rfind( char ch, <strong>size_type</strong> index );</pre>  <p>The rfind() function either:</p>  <ul>    <li>returns the location of the first occurrence of <em>str</em> in    the current string, doing a reverse search from <em>index</em>,    string::npos if nothing is found,</li>    <li>returns the location of the first occurrence of <em>str</em> in    the current string, doing a reverse search from <em>index</em>,    searching at most <em>num</em> characters, string::npos if nothing    is found,</li>    <li>or returns the location of the first occurrence of <em>ch</em>    in the current string, doing a reverse search from <em>index</em>,    string::npos if nothing is found.</li>  </ul>  <p>For example, in the following code, the first call to rfind()  returns string::npos, because the target word is not within the first  8 characters of the string. However, the second call returns 9,  because the target word is within 20 characters of the beginning of  the string.</p>  <pre class="example-code">   int loc;   string s = &quot;My cat&#39;s breath smells like cat food.&quot;;   loc = s.rfind( &quot;breath&quot;, 8 );   cout &lt;&lt; &quot;The word breath is at index &quot; &lt;&lt; loc &lt;&lt; endl;   loc = s.rfind( &quot;breath&quot;, 20 );   cout &lt;&lt; &quot;The word breath is at index &quot; &lt;&lt; loc &lt;&lt; endl;           </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="find.html">find</a><br>    <a href="find_first_not_of.html">find_first_not_of</a><br>    <a href="find_first_of.html">find_first_of</a><br>    <a href="find_last_not_of.html">find_last_not_of</a><br>    <a href="find_last_of.html">find_last_of</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    size  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  <strong>size_type</strong> size() const;</pre>  <p>The size() function returns the number of elements in the current  string.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="capacity.html">capacity</a><br>    <a href="empty.html">empty</a><br>    <a href="length.html">length</a><br>    <a href="max_size.html">max_size</a><br>    <a href="resize.html">resize</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    String constructors  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  string();  string( const string&amp; s );  string( <strong>size_type</strong> length, const char&amp; ch );  string( const char* str );  string( const char* str, <strong>size_type</strong> length );  string( const string&amp; str, <strong>size_type</strong> index, <strong>size_type</strong> length );  string( <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end );  ~string();</pre>  <p>The string constructors create a new string containing:</p>  <ul>    <li>nothing; an empty string,</li>    <li>a copy of the given string <em>s</em>,</li>    <li><em>length</em> copies of <em>ch</em>,</li>    <li>a duplicate of <em>str</em> (optionally up to <em>length</em>    characters long),</li>    <li>a substring of <em>str</em> starting at <em>index</em> and    <em>length</em> characters long</li>    <li>a string of characterss denoted by the <em>start</em> and    <em>end</em> iterators</li>  </ul>  <p>For example,</p>  <pre class="example-code">   string str1( 5, &#39;c&#39; );   string str2( &quot;Now is the time...&quot; );   string str3( str2, 11, 4 );   cout &lt;&lt; str1 &lt;&lt; endl;   cout &lt;&lt; str2 &lt;&lt; endl;   cout &lt;&lt; str3 &lt;&lt; endl;            </pre>  <p>displays</p>  <pre class="example-code">   ccccc   Now is the time...   time         </pre>  <p>The string constructors usually run in <a href=  "../complexity.html">linear time</a>, except the empty constructor,  which runs in <a href="../complexity.html">constant time</a>.</p>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    String operators  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  bool operator==(const string&amp; c1, const string&amp; c2);  bool operator!=(const string&amp; c1, const string&amp; c2);  bool operator&lt;(const string&amp; c1, const string&amp; c2);  bool operator&gt;(const string&amp; c1, const string&amp; c2);  bool operator&lt;=(const string&amp; c1, const string&amp; c2);  bool operator&gt;=(const string&amp; c1, const string&amp; c2);  string operator+(const string&amp; s1, const string&amp; s2 );  string operator+(const char* s, const string&amp; s2 );  string operator+( char c, const string&amp; s2 );  string operator+( const string&amp; s1, const char* s );  string operator+( const string&amp; s1, char c );  ostream&amp; operator&lt;&lt;( ostream&amp; os, const string&amp; s );  istream&amp; operator&gt;&gt;( istream&amp; is, string&amp; s );  string&amp; operator=( const string&amp; s );  string&amp; operator=( const char* s );  string&amp; operator=( char ch );  char&amp; operator[]( <strong>size_type</strong> index );</pre>  <p>C++ strings can be compared and assigned with the standard  comparison operators: ==, !=, &lt;=, &gt;=, &lt;, &gt;, and =.  Performing a comparison or assigning one string to another takes  <a href="../complexity.html">linear time</a>.</p>  <p>Two strings are equal if:</p>  <pre class="example-code">  1. Their size is the same, and  2. Each member in location i in one string is equal to the the member in location i in the other string.              </pre>  <p>Comparisons among strings are done lexicographically.</p>  <p>In addition to these normal (C++ Multimaps) <a href=  "../cppmultimap/multimap_operators.html">Multimap operators</a>,  strings can also be concatenated with the + operator and fed to the  C++ I/O stream classes with the &lt;&lt; and &gt;&gt; operators.</p>  <p>For example, the following code concatenates two strings and  displays the result:</p>  <pre class="example-code"> string s1 = &quot;Now is the time...&quot;; string s2 = &quot;for all good men...&quot;; string s3 = s1 + s2; cout &lt;&lt; &quot;s3 is &quot; &lt;&lt; s3 &lt;&lt; endl;            </pre>  <p>Futhermore, strings can be assigned values that are other  strings, character arrays, or even single characters. The following  code is perfectly valid:</p>  <pre class="example-code"> char ch = &#39;N&#39;; string s; s = ch;         </pre>  <p>Individual characters of a string can be examined with the []  operator, which runs in <a href="../complexity.html">constant  time</a>.</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>    <a href="c_str.html">c_str</a><br>    <a href="compare.html">compare</a><br>    <a href="data.html">data</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    substr  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  string substr( <strong>size_type</strong> index, <strong>size_type</strong> num = npos );</pre>  <p>The substr() function returns a substring of the current string,  starting at <em>index</em>, and <em>num</em> characters long. If  <em>num</em> is omitted, it will default to string::npos, and the  substr() function will simply return the remainder of the string  starting at <em>index</em>.</p>  <p>For example:</p>  <pre class="example-code">   string s(&quot;What we have here is a failure to communicate&quot;);   string sub = s.substr(21);   cout &lt;&lt; &quot;The original string is &quot; &lt;&lt; s &lt;&lt; endl;   cout &lt;&lt; &quot;The substring is &quot; &lt;&lt; sub &lt;&lt; endl;              </pre>  <p>displays</p>  <pre class="example-code">   The original string is What we have here is a failure to communicate   The substring is a failure to communicate            </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="copy.html">copy</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    swap  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;string&gt;  void swap( container&amp; from );</pre>  <p>The swap() function exchanges the elements of the current string  with those of <em>from</em>. This function operates in <a href=  "../complexity.html">constant time</a>.</p>  <p>For example, the following code uses the swap() function to  exchange the values of two strings:</p>  <pre class="example-code">   string first( &quot;This comes first&quot; );   string second( &quot;And this is second&quot; );   first.swap( second );   cout &lt;&lt; first &lt;&lt; endl;   cout &lt;&lt; second &lt;&lt; endl;          </pre>  <p>The above code displays:</p>  <pre class="example-code">   And this is second   This comes first             </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    (C++ Lists) <a href="../cpplist/splice.html">splice</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr></body></html>

⌨️ 快捷键说明

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