📄 all.html
字号:
string& replace( <strong>size_type</strong> index1, <strong>size_type</strong> num1, const string& str, <strong>size_type</strong> index2, <strong>size_type</strong> num2 ); string& replace( <strong>size_type</strong> index, <strong>size_type</strong> num, const char* str ); string& replace( <strong>size_type</strong> index, <strong>size_type</strong> num1, const char* str, <strong>size_type</strong> num2 ); string& replace( <strong>size_type</strong> index, <strong>size_type</strong> num1, <strong>size_type</strong> num2, char ch ); string& replace( iterator start, iterator end, const string& str ); string& replace( iterator start, iterator end, const char* str ); string& replace( iterator start, iterator end, const char* str, <strong>size_type</strong> num ); string& 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 "They say he carved it himself...find your soul-mate, Homer."</p> <pre class="example-code"> 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> <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 <string> 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 <string> void resize( <strong>size_type</strong> size, const <a href="../containers.html">TYPE</a>& 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 & 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 <string> <strong>size_type</strong> rfind( const string& 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 = "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> <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 <string> <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 <string> string(); string( const string& s ); string( <strong>size_type</strong> length, const char& ch ); string( const char* str ); string( const char* str, <strong>size_type</strong> length ); string( const string& 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, 'c' ); string str2( "Now is the time..." ); string str3( str2, 11, 4 ); cout << str1 << endl; cout << str2 << endl; cout << str3 << 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 <string> bool operator==(const string& c1, const string& c2); bool operator!=(const string& c1, const string& c2); bool operator<(const string& c1, const string& c2); bool operator>(const string& c1, const string& c2); bool operator<=(const string& c1, const string& c2); bool operator>=(const string& c1, const string& c2); string operator+(const string& s1, const string& s2 ); string operator+(const char* s, const string& s2 ); string operator+( char c, const string& s2 ); string operator+( const string& s1, const char* s ); string operator+( const string& s1, char c ); ostream& operator<<( ostream& os, const string& s ); istream& operator>>( istream& is, string& s ); string& operator=( const string& s ); string& operator=( const char* s ); string& operator=( char ch ); char& operator[]( <strong>size_type</strong> index );</pre> <p>C++ strings can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, 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 << and >> operators.</p> <p>For example, the following code concatenates two strings and displays the result:</p> <pre class="example-code"> string s1 = "Now is the time..."; string s2 = "for all good men..."; string s3 = s1 + s2; cout << "s3 is " << s3 << 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 = 'N'; 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 <string> 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("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 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 <string> void swap( container& 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( "This comes first" ); string second( "And this is second" ); first.swap( second ); cout << first << endl; cout << second << 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 + -