cppstring_details.html
来自「ssd5 数据结构的课件」· HTML 代码 · 共 877 行 · 第 1/3 页
HTML
877 行
<pre> <a href="iterators.html">iterator</a> insert( <a href="iterators.html">iterator</a> i, const char &ch ); basic_string &insert( size_type index, const basic_string &str ); basic_string &insert( size_type index, const char *str ); basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num ); basic_string &insert( size_type index, const char *str, size_type num ); basic_string &insert( size_type index, size_type num, char ch ); void insert( <a href="iterators.html">iterator</a> i, size_type num, const char &ch ); void insert( <a href="iterators.html">iterator</a> i, <a href="iterators.html">iterator</a> start, <a href="iterators.html">iterator</a> end );</pre> </td> </tr> </table> <p>The very multi-purpose insert() function either:</p> <ul> <li>inserts <i>ch</i> before the character denoted by <i>i</i>,</li> <li>inserts <i>str</i> into the current string, at location <i>index</i>,</li> <li>inserts a substring of <i>str</i> (starting at <i>index2</i> and <i>num</i> characters long) into the current string, at location <i>index1</i>,</li> <li>inserts <i>num</i> characters of <i>str</i> into the current string, at location <i>index</i>,</li> <li>inserts <i>num</i> copies of <i>ch</i> into the current string, at location <i>index</i>,</li> <li>inserts <i>num</i> copies of <i>ch</i> into the current string, before the character denoted by <i>i</i>,</li> <li>or inserts the characters denoted by <i>start</i> and <i>end</i> into the current string, before the character specified by <i>i</i>.</li> </ul> <br> <br> <i>Related topics:</i><br> <strong><a href="#replace">replace()</a></strong> <hr> <h2><a name="length">length</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type length();</pre> </td> </tr> </table> <p>The function length() returns the length of the current string. This number should be the same as the one returned from <a href="#size">size()</a>.</p> <i>Related topics:</i><br> <strong><a href="#size">size()</a></strong> <hr> <h2><a name="max_size">max_size</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type max_size();</pre> </td> </tr> </table> <p>The max_size() function returns the maximum number of characters that the string can hold.</p> <hr> <h2><a name="rbegin">rbegin</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> const <a href="iterators.html">reverse_iterator</a> rbegin();</pre> </td> </tr> </table> <p>The function rbegin() returns a reverse <a href="iterators.html">iterator</a> to the end of the current string.</p> <i>Related topics:</i><br> <strong><a href="#rend">rend()</a></strong> <hr> <h2><a name="rend">rend</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> const <a href="iterators.html">reverse_iterator</a> rend();</pre> </td> </tr> </table> <p>The rend() function returns a reverse <a href="iterators.html">iterator</a> to the start of the current string.</p> <i>Related topics:</i><br> <strong><a href="#rbegin">rbegin()</a></strong> <hr> <h2><a name="replace">replace</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> basic_string &replace( size_type index, size_type num, const basic_string &str ); basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2, size_type num2 ); basic_string &replace( size_type index, size_type num, const char *str ); basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 ); basic_string &replace( size_type index, size_type num1, size_type num2, char ch ); basic_string &replace( <a href="iterators.html">iterator</a> start, <a href="iterators.html">iterator</a> end, const basic_string &str ); basic_string &replace( <a href="iterators.html">iterator</a> start, <a href="iterators.html">iterator</a> end, const char *str ); basic_string &replace( <a href="iterators.html">iterator</a> start, <a href="iterators.html">iterator</a> end, const char *str, size_type num ); basic_string &replace( <a href="iterators.html">iterator</a> start, <a href="iterators.html">iterator</a> end, size_type num, char ch );</pre> </td> </tr> </table> <p>The function replace() either:</p> <ul> <li>replaces characters of the current string with up to <i>num</i> characters from <i>str</i>, beginning at <i>index</i>,</li> <li>replaces up to <i>num1</i> characters of the current string (starting at <i>index1</i>) with up to <i>num2</i> characters from <i>str</i> beginning at <i>index2</i>,</li> <li>replaces up to <i>num</i> characters of the current string with characters from <i>str</i>, beginning at <i>index</i> in <i>str</i>,</li> <li>replaces up to <i>num1</i> characters in the current string (beginning at <i>index1</i>) with <i>num2</i> characters from <i>str</i> beginning at <i>index2</i>,</li> <li>replaces up to <i>num1</i> characters in the current string (beginning at <i>index</i>) with <i>num2</i> copies of <i>ch</i>,</li> <li>replaces the characters in the current string from <i>start</i> to <i>end</i> with <i>str</i>,</li> <li>replaces characters in the current string from <i>start</i> to <i>end</i> with <i>num</i> characters from <i>str</i>,</li> <li>or replaces the characters in the current string from <i>start</i> to <i>end</i> with <i>num</i> copies of <i>ch</i>.</li> </ul> For example, the following code displays the string "They say he carved it himself...find your soul-mate, Homer." <pre> string s = "They say he carved it himself...from a BIGGER spoon"; string s2 = "find your soul-mate, Homer."; s.replace( 32, s2.length(), s2 ); cout << s << endl; </pre> <br> <br> <i>Related topics:</i><br> <strong><a href="#insert">insert()</a></strong> <hr> <h2><a name="reserve">reserve</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void reserve( size_type num );</pre> </td> </tr> </table> <p>The function reserve() sets the <a href="#capacity">capacity()</a> of the current string to at least <i>num</i>.</p> <i>Related topics:</i><br> <strong><a href="#capacity">capacity()</a></strong> <hr> <h2><a name="resize">resize</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void resize( size_type num ); void resize( size_type num, char ch );</pre> </td> </tr> </table> <p>The resize() function changes the size of the current string to <i>num</i>, optionally padding any new space with copies of <i>ch</i>.</p> <hr> <h2><a name="rfind">rfind</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type rfind( const basic_string &str, size_type index ); size_type rfind( const char *str, size_type index ); size_type rfind( const char *str, size_type index, size_type num ); size_type rfind( char ch, size_type index );</pre> </td> </tr> </table> <p>The rfind() function either:</p> <ul> <li>returns the location of the first occurrence of <i>str</i> in the current string, doing a reverse search from <i>index</i>, <strong>string::npos</strong> if nothing is found,</li> <li>returns the location of the first occurrence of <i>str</i> in the current string, doing a reverse search from <i>index</i>, searching at most <i>num</i> characters, <strong>string::npos</strong> if nothing is found,</li> <li>or returns the location of the first occurrence of of <i>ch</i> in the current string, doing a recerse search from <i>index</i>, <strong>string::npos</strong> if nothing is found.</li> </ul> For example, in the following code, the first call to rfind() returns <strong>string::npos</strong>, 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. <pre> int loc; string s = "My cat's breath smells like cat food."; loc = s.rfind( "breath", 8 ); cout << "The word breath is at index " << loc << endl; loc = s.rfind( "breath", 20 ); cout << "The word breath is at index " << loc << endl; </pre> <br> <br> <i>Related topics:</i><br> <strong><a href="#find">find()</a></strong> <hr> <h2><a name="size">size</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type size();</pre> </td> </tr> </table> <p>The size() function returns the number of characters currently in the string.</p> <i>Related topics:</i><br> <strong><a href="#length">length()</a>, <a href="#max_size">max_size()</a></strong> <hr> <h2><a name="substr">substr</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> basic_string substr( size_type index, size_type num = npos );</pre> </td> </tr> </table> <p>The substr() function returns a substring of the current string, starting at <i>index</i>, and <i>num</i> characters long. If <i>num</i> is omitted, it will default to <strong>string::npos</strong>, and the substr() function will simply return the remainder of the string starting at <i>index</i>. For example:</p><pre> string s("What we have here is a failure to communicate"); string sub = s.substr(21); cout << "The original string is " << s << endl; cout << "The substring is " << sub << endl;</pre> <p>displays</p><pre> The original string is What we have here is a failure to communicate The substring is a failure to communicate</pre> <hr> <h2><a name="swap">swap</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void swap( basic_string &str );</pre> </td> </tr> </table> <p>The function swap() switches <i>str</i> with the current string. For example:</p><pre> string first( "This comes first" ); string second( "And this is second" ); first.swap( second ); cout << first << endl; cout << second << endl;</pre> <p>displays</p><pre> And this is second This comes first</pre> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?